Total Words: ~1,300
Python syntax refers to the set of rules that define how Python code is written and structured. Unlike many programming languages, Python emphasizes readability and simplicity, using indentation (whitespace) to define code blocks instead of braces {}.
Key characteristics of Python syntax:
Example:
python
Copy
# This is a comment
if 5 > 2:
print("Five is greater than two!") # Indented block
Variables store data values. Python uses dynamic typing, meaning you don’t need to declare a variable’s type explicitly.
Syntax:
python
Copy
variable_name = value
Example:
python
Copy
name = "Alice"
age = 25
is_student = True
Type | Description | Example |
int | Integer values | 10, -5 |
float | Decimal values | 3.14, -0.5 |
str | Text (enclosed in quotes) | "Hello", 'Python' |
bool | Boolean (True/False) | True, False |
NoneType | Represents absence of value | None |
Type Checking:
python
Copy
print(type(age)) # Output: <class 'int'>
print(type(is_student)) # Output: <class 'bool'>
Operator | Description | Example |
+ | Addition | 5 + 3 → 8 |
- | Subtraction | 10 - 2 → 8 |
* | Multiplication | 4 * 3 → 12 |
/ | Division | 10 / 2 → 5.0 |
** | Exponentiation | 2 ** 3 → 8 |
% | Modulus (remainder) | 10 % 3 → 1 |
Operator | Description | Example |
== | Equal to | 5 == 5 → True |
!= | Not equal to | 3 != 5 → True |
> | Greater than | 10 > 5 → True |
< | Less than | 3 < 2 → False |
Operator | Description | Example |
and | Both conditions true | (5 > 2) and (3 < 5) → True |
or | At least one condition true | (5 > 10) or (3 < 5) → True |
not | Inverts the result | not (5 == 5) → False |
Use input() to read user input (returns a string):
python
Copy
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert to integer
Use print() to display output:
python
Copy
print("Hello,", name)
print(f"You are {age} years old.") # f-string (formatted string)
if 5 > 2:
age = "25"
Let’s build a calculator that adds two numbers:
python
Copy
# Get user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Calculate sum
sum = num1 + num2
# Display result
print(f"{num1} + {num2} = {sum}")
Output:
Copy
Enter first number: 3.5
Enter second number: 4.2
3.5 + 4.2 = 7.7
In Week 2, we’ll explore Conditions and Control Flow, including if-else statements and logical operators to create dynamic programs!
Sql
https://www.youtube.com/watch?v=27axs9dO7AE&pp=ygUMaW50cm8gdG8gc3Fs
https://www.youtube.com/watch?v=5tEApCGgpEQ&pp=ygULc3FsIHF1ZXJpZXM%3D
https://www.youtube.com/watch?v=jcoJuc5e3RE&pp=ygUaYWdncmVnYXRlIGZ1bmN0aW9ucyBpbiBzcWw%3D
https://www.youtube.com/watch?v=qBMo-AORiFQ&pp=ygUfIGNyZWF0aW4gbXVsdGlwbGUgdGFibGVzIGluIHNxbA%3D%3D
https://www.youtube.com/watch?v=G3lJAxg1cy8&pp=ygUVam9pbmluZyB0YWJsZXMgaW4gc3Fs
https://docs.google.com/document/d/e/2PACX-1vRVhzAGuHANEg-mNWUlaIw5g9nRl1AeOjEbeZTDCyIrMNaEmohZCdTFVX2qD4xfGBNhg_r9tfFqQbtx/pub?embedded=true