Beginner's Guide to Python Programming
Python is a widely used programming language known for its simplicity
and readability. This lesson will introduce you to the basics of Python,
providing clear explanations and examples to help you get started.
---
## **Lesson 1: Introduction to Python**
### **What is Python?**
Python is a high-level programming language used for web development,
data analysis, artificial intelligence, automation, and more. It is popular
because:
- It has a clean and easy-to-read syntax.
- It supports multiple programming paradigms (procedural, object-
oriented, and functional).
- It has a vast collection of libraries and frameworks.
### **Setting Up Python**
Before writing Python code, ensure Python is installed on your computer:
1. Download Python from [python.org](https://www.python.org/).
2. Follow the installation instructions.
3. Open a terminal or command prompt and type `python --version` to
check installation.
You can write Python code in:
- **Interactive Mode**: Type `python` in the terminal to enter an
interactive shell.
- **Script Mode**: Write code in a file with a `.py` extension and run it.
---
## **Lesson 2: Python Syntax and Basics**
### **Printing Output**
Python uses the `print()` function to display text:
```python
print("Hello, world!")
```
Output:
```
Hello, world!
```
### **Variables and Data Types**
Variables store data, and Python automatically determines their type:
```python
name = "Alice" # String
age = 25 # Integer
height = 5.7 # Float
is_student = True # Boolean
```
### **User Input**
You can take input from users using `input()`:
```python
user_name = input("Enter your name: ")
print("Hello, " + user_name + "!")
```
---
## **Lesson 3: Operators and Control Flow**
### **Arithmetic Operators**
Python supports basic arithmetic operations:
```python
x = 10
y=3
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division
print(x % y) # Modulus
```
### **Conditional Statements**
Python uses `if`, `elif`, and `else` for decision-making:
```python
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
elif age > 12:
print("You are a teenager.")
else:
print("You are a child.")
```
### **Loops**
Loops help execute repeated tasks:
- **For loop** (iterates over a sequence):
```python
for i in range(5):
print("Iteration:", i)
```
- **While loop** (runs as long as a condition is true):
```python
count = 0
while count < 5:
print("Count:", count)
count += 1
```
---
## **Lesson 4: Functions and Lists**
### **Defining Functions**
Functions allow code reuse and better structure:
```python
def greet(name):
print("Hello,", name)
greet("Alice")
```
### **Lists**
Lists store multiple values:
```python
fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0]) # Accessing elements
fruits.append("Orange") # Adding an element
print(fruits)
```
---
## **Lesson 5: Dictionaries and Error Handling**
### **Dictionaries**
Dictionaries store key-value pairs:
```python
person = {"name": "Alice", "age": 25, "city": "New York"}
print(person["name"])
```
### **Handling Errors**
Use `try` and `except` to prevent crashes:
```python
try:
num = int(input("Enter a number: "))
print("You entered:", num)
except ValueError:
print("Invalid input! Please enter a number.")
```
---
## **Review Questions**
1. What are the advantages of using Python?
2. How do you print text in Python?
3. What function is used to get user input?
4. What are conditional statements used for?
5. Name two types of loops in Python and give examples.
6. How do you define a function in Python?
7. What is the difference between lists and dictionaries?
8. What keyword is used for error handling?