0% found this document useful (0 votes)
29 views10 pages

Basic Python Programming Tutorial

Uploaded by

Temp Temp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
29 views10 pages

Basic Python Programming Tutorial

Uploaded by

Temp Temp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Beginner’s Python Programming Tutorial

Table of Contents

1. Introduction
2. Importance of Learning Python
3. Installing Python
4. Program Structure
5. Variables and Constants
6. Data Types
7. Operators
8. Control Structures
9. Functions
10. Lists and Dictionaries
11. Error Types in Python
12. File Handling
13. Debugging Techniques
14. Complex Project: Employee Management System
15. Conclusion

1. Introduction

Python is a versatile, high-level, interpreted programming language. It’s widely used in


web development, data science, artificial intelligence, automation, and more. Known for
its readability and simplicity, Python is an excellent choice for beginners and
professionals alike.

2. Importance of Learning Python

Python is essential for developers due to its readability, flexibility, and vast range of
applications.

https://zedicthub.blogspot.com/

1
 Beginner-Friendly: Ideal language for newcomers.
 High Demand: Valued in various industries.
 Large Community and Libraries: Extensive support and resources.
 Multi-Purpose Language: Ideal for data science, AI, web development, etc.

3. Installing Python
Windows, macOS, and Linux

1. Visit python.org, download the appropriate installer, and follow installation steps.
2. Verify installation by typing python --version or python3 --version in the terminal.

Once installed, Python files can be saved with a .py extension and executed via command
line.

4. Program Structure
Example Program
# Filename: hello_world.py
print("Hello, World!") # Output a greeting

Output:

Hello, World!
Exercise

Write a Python program to print your name and age.

Solution:

name = "John Doe"


age = 25
print("Name:", name)
print("Age:", age)

Output:

Name: John Doe


Age: 25

https://zedicthub.blogspot.com/

2
5. Variables and Constants

Variables hold data values, and constants are generally written in uppercase to signify
they shouldn’t change.

age = 25
PI = 3.14159 # Constant
print("Age:", age, "PI:", PI)

Output:

Age: 25 PI: 3.14159


Exercise

Define a variable city and assign it the name of a city. Print the variable.

Solution:

city = "New York"


print("City:", city)

Output:

City: New York

6. Data Types

Python supports multiple data types, including int, float, str, bool, and lists or
dictionaries.

age = 21 # Integer
height = 5.9 # Float
name = "Alice" # String
is_student = True # Boolean
print(age, height, name, is_student)

Output:

21 5.9 Alice True


Exercise

Create a variable price with a float value and product with a string value. Print both.
https://zedicthub.blogspot.com/

3
Solution:

price = 15.99
product = "Book"
print("Product:", product, "Price:", price)

Output:

Product: Book Price: 15.99

7. Operators

Python operators perform various operations like arithmetic, relational, and logical
operations.

x = 10
y=3
print("Sum:", x + y) # Addition
print("Difference:", x - y) # Subtraction
print("Product:", x * y) # Multiplication
print("Division:", x / y) # Division

Output:

Sum: 13
Difference: 7
Product: 30
Division: 3.3333333333333335
Exercise

Using two numbers of your choice, calculate and print the sum, difference, and product.

Solution:

a=8
b=5
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)

https://zedicthub.blogspot.com/

4
Output:

Sum: 13
Difference: 3
Product: 40

8. Control Structures

Control structures manage program flow, like if-else and loops (for, while).

score = 75
if score >= 60:
print("Passed")
else:
print("Failed")

Output:

Passed

Exercise

Write a program that checks if a number is even or odd.

Solution:

number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd")

Output:

Odd

https://zedicthub.blogspot.com/

5
9. Functions

Functions allow reusable code blocks to perform specific tasks.

def add(a, b):


return a + b

print("Sum:", add(10, 5))

Output:

Sum: 15
Exercise

Create a function multiply that takes two numbers and returns their product. Call the
function with values 4 and 5 and print the result.

Solution:

def multiply(x, y):


return x * y

print("Product:", multiply(4, 5))

Output:

Product: 20

10. Lists and Dictionaries

Lists store multiple values, while dictionaries store key-value pairs.

# List
numbers = [10, 20, 30]
print(numbers[0]) # Access the first element

# Dictionary
student = {"name": "Alice", "age": 21}
print(student["name"]) # Access value by key
https://zedicthub.blogspot.com/

6
Output:

Alice
Exercise

Create a list of three fruits and print the first item. Create a dictionary for a book with
keys title and author and print the author.

Solution:

# List
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])

# Dictionary
book = {"title": "1984", "author": "George Orwell"}
print("Author:", book["author"])

Output:

Apple
Author: George Orwell

11. Error Types in Python

Different errors help understand and debug code:

 Syntax Errors: Mistakes in syntax.


 Indentation Errors: Incorrect indentation.
 Type Errors: Incompatible data types.
 Value Errors: Invalid values.
 Runtime Errors: Errors during execution.
 Logical Errors: Code runs but gives incorrect results.

12. File Handling

File handling allows reading/writing to external files.

# Writing to a file
with open("data.txt", "w") as file:
https://zedicthub.blogspot.com/

7
file.write("Hello, File!")

# Reading from a file


with open("data.txt", "r") as file:
print(file.read())

Output:

Hello, File!
Exercise

Write "Python is fun!" to a file named fun.txt and then read it back.

Solution:

# Writing to a file
with open("fun.txt", "w") as file:
file.write("Python is fun!")

# Reading from a file


with open("fun.txt", "r") as file:
print(file.read())

Output:

Python is fun!

13. Debugging Techniques

Effective debugging is essential in programming. Techniques include:

 Print Statements: Track variables.


 Breakpoints: Pause code execution.
 Error Logging: Track complex issues.

14. Complex Project: Employee Management System

This Employee Management System adds, views, and searches employee details.

# Employee Management System

https://zedicthub.blogspot.com/

8
employees = []

def add_employee(id, name, age):


employees.append({"id": id, "name": name, "age": age})

def display_employees():
for emp in employees:
print(f"ID: {emp['id']}, Name: {emp['name']}, Age: {emp['age']}")

def search_employee(id):
for emp in employees:
if emp["id"] == id:
print(f"Found: {emp['name']}, Age: {emp['age']}")
return
print("Employee not found.")

# Adding employees
add_employee(1, "Alice", 30)
add_employee(2, "Bob", 25)

# Display all employees


print("All Employees:")
display_employees()

# Search for an employee


print("Search Employee by ID:")
search_employee(1)

Output: ``

` All Employees: ID: 1, Name: Alice, Age: 30 ID: 2, Name: Bob, Age: 25 Search Employee
by ID: Found: Alice, Age: 30

---

## 15. Conclusion

This tutorial covered fundamental Python programming concepts, from basic syntax to
more complex structures. Practicing through examples and exercises will build your
confidence in coding.
https://zedicthub.blogspot.com/

9
### Additional Practice Exercises
1. Create a program that converts Celsius to Fahrenheit.
2. Write a function to find the factorial of a number.
3. Develop a simple calculator using functions for addition, subtraction, multiplication,
and division.

https://zedicthub.blogspot.com/

10

You might also like