1. What Is Python? How Does the Python Interpreter Work?

1
demo.py

2. A Simple Python Program

Example:

1
print("Hello, Python!")

Explanation:

3. Objects in Python

Example:

1
2
3
x = 10        # int
y = 3.14      # float
name = "AI"   # str

4. Identifiers and Assignment

Python assigns references, not copies of values.

5. Identifier Rules

Case-sensitive:

Can contain:

6. Dynamic Typing

Python uses dynamic typing:

Example:

1
2
x = 10        # int
x = "hello"   # str

Variables do not have types; objects do

7. Objects and Constructors

Creating Objects (Instantiation)

General syntax:

1
obj = ClassName()

Examples:

1
2
numbers = list()
value = int()

Some objects are created using literals:

1
2
3
a = 10        # int
b = 3.14      # float
c = "text"    # str

8. Functions vs. Methods

Difference:

9. Immutable and Mutable Types

Example:

1
2
x = 10
x = x + 1   # creates a new object

9.1. Boolean Type (bool)

Only two Boolean values:

Type conversion:

1
2
3
4
bool(0)        # False
bool(5)        # True
bool("")       # False
bool("hi")     # True

9.2. Integer Type (int)

Supports integers of arbitrary size

Type conversion:

1
2
3
int(3.14)   # 3
int(-3.9)   # -3
int("137")  # 137

9.3. Floating-Point Type (float)

Examples:

1
2
2.0
6.022e23

Type conversion:

1
2
float(2)       # 2.0
float("3.14")  # 3.14

9.4. Lists

Example:

1
2
colors = ["red", "green", "blue"]
colors[0]   # "red"

Creating lists:

1
2
list()
list("hello")  # ['h', 'e', 'l', 'l', 'o']

9.5. Tuples

Similar to lists but immutable

Examples:

1
2
t = (1, 2, 3)
single = (17,)

9.6. Strings (str)

1
2
3
4
5
"hello"
'hello'
"""multi
line
string"""
  1. Sets

Example:

1
2
s = {"red", "green", "blue"}
empty = set()

{} creates an empty dictionary, not a set

9.7. Dictionaries (dict)

Example:

1
lang = {"ga": "Irish", "de": "German"}

Alternative:

1
dict([("ga", "Irish"), ("de", "German")])

10. Expressions and Operators

Operators behave differently depending on operand types:

1
2
3 + 4        # 7
"a" + "b"    # "ab"

10.1. Logical Operators

Example:

1
False and func()   # func() is not executed

10.2. Equality and Identity

a == b # value equality a is b # same object in memory

10.3. Arithmetic Operators

Example:

1
2
5 / 2   # 2.5
5 // 2  # 2

10.3. Bitwise Operators

10.4. Sequence Operators

Supported by str, list, tuple:

10.5. Sequence Comparisons

Compared lexicographically (left to right):

1
[5, 6, 9] < [5, 7]   # True

10.6. Set Operators

10.7. Dictionary Operators

1
"ga" in lang

10.8. Operator Precedence