Basic Python Programming Tutorial
Basic 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 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
Solution:
Output:
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:
Define a variable city and assign it the name of a city. Print the variable.
Solution:
Output:
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:
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:
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
Solution:
number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd")
Output:
Odd
https://zedicthub.blogspot.com/
5
9. Functions
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:
Output:
Product: 20
# 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
# Writing to a file
with open("data.txt", "w") as file:
https://zedicthub.blogspot.com/
7
file.write("Hello, File!")
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!")
Output:
Python is fun!
This Employee Management System adds, views, and searches employee details.
https://zedicthub.blogspot.com/
8
employees = []
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)
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