Python Programming
Python Basics
Variables: Storing Information
- Variables are containers that store data. Think of them as labeled boxes.
- Example:
- `name = 'Ahmed'`
- `age = 25`
- `salary = 50000`
- No need to declare types—Python is smart and figures it out automatically!
Data Types: Different Kinds of Information
- String (text): `name = 'Pakistan'` – Always use quotes
- Integer (whole numbers): `age = 30`
- Float (decimal numbers): `price = 99.99`
- Boolean (True/False): `is_student = True`
- You can check type with: `print(type(age))` → Shows `<class 'int'>`
Basic Operators
- Math Operators:
- `+` (add), `-` (subtract), `*` (multiply), `/` (divide), `%` (remainder)
- Example: `total = 100 + 50` → `total` is now 150
- Comparison Operators:
- `==` (equal), `!=` (not equal), `>` (greater), `<` (less)
- Example: `age > 18` → Returns `True` or `False`
Getting User Input
- Use `input()` to ask users for information:
- `name = input('What is your name? ')`
- `print('Hello, ' + name)`
- Note: `input()` always returns text. To get numbers, convert them:
- `age = int(input('Enter your age: '))`
Print Output
- Display information using `print()`:
- `print('Hello!')` → Simple text
- `print('My age is', age)` → Mix text and variables
- `print(f'My name is {name} and I am {age} years old')` → F-strings (modern way)