Python Basics

Variables and Constants
Variables
# Variables
x = 10
y = 20
z = x + y
Constants
# Constants
PI = 3.14159
GRAVITY = 9.8

# Note: In Python, constants are usually named in all caps, but they can still be modified.
# There is no true constant declaration in Python.
Reference Types vs. Value Types
# Value types (immutable types)
# Examples: int, float, bool, string, tuple, frozenset
a = 10
b = a
b = 20
print(a)  # Output: 10
print(b)  # Output: 20

# Reference types (mutable types)
# Examples: list, set, dict
list1 = [1, 2, 3]
list2 = list1
list2.append(4)
print(list1)  # Output: [1, 2, 3, 4]
print(list2)  # Output: [1, 2, 3, 4]

# Explanation:
# - Value types: Changes to one variable do not affect another.
# - Reference types: Changes to one variable affect the other, as they refer to the same object.
Classes and Instances
# Define a class
class MyClass:
    # Constructor
    def __init__(self, attribute1, attribute2):
        self.attribute1 = attribute1
        self.attribute2 = attribute2
    
    # Method
    def my_method(self):
        return self.attribute1 + self.attribute2

# Create an instance of the class
instance = MyClass(10, 20)
print(instance.my_method())  # Output: 30
Data Structures
Arrays (Lists)
# Arrays (Lists)
# Create a list
my_list = [1, 2, 3, 4, 5]

# Access elements
print(my_list[0])  # Output: 1

# Modify elements
my_list[1] = 20

# Append elements
my_list.append(6)

# Iterate through a list
for element in my_list:
    print(element)
Sets
# Sets
# Create a set
my_set = {1, 2, 3, 4, 5}

# Add elements
my_set.add(6)

# Remove elements
my_set.remove(3)

# Check membership
print(2 in my_set)  # Output: True

# Iterate through a set
for element in my_set:
    print(element)
Hash Tables (Dictionaries)
# Hash Tables (Dictionaries)
# Create a dictionary
my_dict = {'key1': 'value1', 'key2': 'value2'}

# Access elements
print(my_dict['key1'])  # Output: value1

# Modify elements
my_dict['key1'] = 'new_value1'

# Add new key-value pairs
my_dict['key3'] = 'value3'

# Iterate through a dictionary
for key, value in my_dict.items():
    print(f'{key}: {value}')
Functions
# Define a function
def my_function(param1, param2):
    return param1 + param2

# Call a function
result = my_function(10, 20)
print(result)  # Output: 30
Control Statements
If Statements
# If Statements
if condition:
    # Code to execute if condition is True
elif another_condition:
    # Code to execute if another_condition is True
else:
    # Code to execute if all conditions are False
For Loops
# For Loops
# For loop
for i in range(5):
    print(i)  # Output: 0 1 2 3 4

# Iterate through a list
for element in my_list:
    print(element)
While Loops
# While Loops
# While loop
count = 0
while count < 5:
    print(count)  # Output: 0 1 2 3 4
    count += 1
Skip (continue), Break, Return
# Skip (continue)
for i in range(5):
    if i == 2:
        continue  # Skip the rest of the code in this iteration
    print(i)  # Output: 0 1 3 4

# Break
for i in range(5):
    if i == 2:
        break  # Exit the loop
    print(i)  # Output: 0 1

# Return
def my_function(param):
    if param < 0:
        return  # Exit the function
    return param * 2

print(my_function(10))  # Output: 20
print(my_function(-1))  # Output: None