Week 1: Python Syntax Basics

Total Words: ~1,300


What is Python Syntax?

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 and Data Types

Variables

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 

Basic Data Types

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'>


Basic Operations

Arithmetic Operators

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

String Operations

Comparison Operators

Operator

Description

Example

==

Equal to

5 == 5 → True

!=

Not equal to

3 != 5 → True

>

Greater than

10 > 5 → True

<

Less than

3 < 2 → False

Logical Operators

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


Input and Output

Getting User Input

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

Printing Output

Use print() to display output:

python

Copy

print("Hello,", name) 

print(f"You are {age} years old.")  # f-string (formatted string)


Common Mistakes & Troubleshooting

  1. Indentation Errors:
  2. python
  3. Copy

if 5 > 2: 

  1. print("Hello")  # ERROR: Missing indentation
    Fix: Indent code blocks consistently.
  2. Incorrect Variable Names:
  3. python
  4. Copy
  5. 2nd_name = "Alice"  # ERROR: Starts with a number
    Fix: Use second_name instead.
  6. Type Errors:
  7. python
  8. Copy

age = "25" 

  1. print(age + 5)  # ERROR: Can't add string and integer
    Fix: Convert types explicitly: print(int(age) + 5).
  2. Forgetting Quotes for Strings:
  3. python
  4. Copy
  5. message = Hello  # ERROR: Hello is undefined
    Fix: Use message = "Hello".

Practice Example: Simple Calculator

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


Key Takeaways


Next Week Preview

In Week 2, we’ll explore Conditions and Control Flow, including if-else statements and logical operators to create dynamic programs!

https://docs.google.com/document/d/e/2PACX-1vT1T8lh2OCv5Jo5497EmcccbtJp3RDTbi2xbZGcskMFyWCoAP8KWwgAj2RkzwMLSLDai2AE4lniX_qC/pub?embedded=true

https://docs.google.com/document/d/e/2PACX-1vR-BsZCGMl3vdkBBYg_mEK5pQfI3dJQU7ZdlOvlrnSrwWChkFeQQj0fDIKhoKVoUPYQPPWthiGKBmT3/pub?embedded=true

https://docs.google.com/document/d/e/2PACX-1vRgjU9GTdk58XZRI_gcWEFmbnNvaBJYXKFhFqLLtEO-mpcyx7ATYsPWsTpoKgM3tU4gL-WmwNVHNTDQ/pub?embedded=true

https://docs.google.com/document/d/e/2PACX-1vT6_9zPqe7SfHRMyPFzW-nROOYV6yLU6N87tLCs2g4guwXP1ksqmv0HVhnU7iHhp5P-8JwyhLmgEDjL/pub?embedded=true

https://docs.google.com/document/d/e/2PACX-1vSkfZKpZa1PtqItVpiqnQmHEFc2KnOvnfrk91gumX0jnJ6O06eZq0d8-j8EvtEuXKxuwkKUc0TpWkGV/pub?embedded=true

Sql

https://www.youtube.com/watch?v=27axs9dO7AE&pp=ygUMaW50cm8gdG8gc3Fs

https://docs.google.com/document/d/e/2PACX-1vTptUOu8tqEb45G9H3rDo2uanfK4oaiqRwSoLwELvNj0gtpHeff7r0hwIiObi6AHh5efQSdyxBeAmor/pub?embedded=true

https://www.youtube.com/watch?v=5tEApCGgpEQ&pp=ygULc3FsIHF1ZXJpZXM%3D

https://docs.google.com/document/d/e/2PACX-1vSF0LgvcUozX1OFjcSHBAJ9GuLCEW4yV-TtJY-Dg4Eb3BErR7MWMpiP5__Egvyn097QqsfK23du5ftp/pub?embedded=true

https://www.youtube.com/watch?v=jcoJuc5e3RE&pp=ygUaYWdncmVnYXRlIGZ1bmN0aW9ucyBpbiBzcWw%3D

https://docs.google.com/document/d/e/2PACX-1vS5hMTZi5MWHaJLvEiRSBhWqlkfDRIKkWyM8pi9Wr2oHC8BBKQO0cOPa9tMBSuwAZEU1To3LXyjieWE/pub?embedded=true

https://www.youtube.com/watch?v=qBMo-AORiFQ&pp=ygUfIGNyZWF0aW4gbXVsdGlwbGUgdGFibGVzIGluIHNxbA%3D%3D

https://docs.google.com/document/d/e/2PACX-1vQX14Nodq6OEiX_LcrMYYNBqa2yZV7YNfe25yijLqSH_TuL7Qy5OhV_YH1RlIjPkBrHdeTvDkY5WICm/pub?embedded=true

https://www.youtube.com/watch?v=G3lJAxg1cy8&pp=ygUVam9pbmluZyB0YWJsZXMgaW4gc3Fs

https://docs.google.com/document/d/e/2PACX-1vRVhzAGuHANEg-mNWUlaIw5g9nRl1AeOjEbeZTDCyIrMNaEmohZCdTFVX2qD4xfGBNhg_r9tfFqQbtx/pub?embedded=true