# Variables in Python
# A variable is a name that refers to a value. In Python, you don't need to declare a variable before using it.
# You simply assign a value to a variable using the assignment operator '='.
# Example of variable assignment
x = 10 # Here, 'x' is a variable that holds the integer value 10
y = 5.5 # 'y' is a variable that holds the float value 5.5
name = "Alice" # 'name' is a variable that holds the string value "Alice"
is_student = True # 'is_student' is a variable that holds the boolean value True
# Rules for assigning variables:
# 1. Variable names must start with a letter (a-z, A-Z) or an underscore (_).
# 2. The rest of the variable name can contain letters, numbers, or underscores.
# 3. Variable names are case-sensitive (e.g., 'age' and 'Age' are different variables).
# 4. Variable names cannot be Python reserved words (e.g., 'if', 'else', 'while', 'for', etc.).
# Examples of valid variable names
age = 25
_age = 30
age_2 = 35
# Examples of invalid variable names
# 2age = 40 # Invalid: starts with a number
# age! = 45 # Invalid: contains a special character
# fetching reserved keywords
import keyword as k
print(k.kwlist)
# Using variables
# You can use variables to store values and perform operations on them.
# Example of using variables in expressions
sum = x + y # Adds the values of 'x' and 'y' and stores the result in 'sum'
print("Sum:", sum) # Output: Sum: 15.5
# Example of updating variable values
x = x + 5 # Updates the value of 'x' by adding 5 to its current value
print("Updated x:", x) # Output: Updated x: 15
# Variable types
# Python is a dynamically-typed language, which means you don't need to declare the type of a variable.
# The type of a variable is determined by the value assigned to it.
# Example of different variable types
integer_var = 10 # Integer type
float_var = 5.5 # Float type
string_var = "Hello" # String type
boolean_var = True # Boolean type
# You can check the type of a variable using the 'type()' function
print("Type of integer_var:", type(integer_var)) # Output: <class 'int'>
print("Type of float_var:", type(float_var)) # Output: <class 'float'>
print("Type of string_var:", type(string_var)) # Output: <class 'str'>
print("Type of boolean_var:", type(boolean_var)) # Output: <class 'bool'>
# Lists in Python
# Lists are ordered, mutable collections of items. They allow duplicate elements.
# Example of a list
fruits = ["apple", "banana", "cherry", "apple"]
print("List:", fruits) # Output: List: ['apple', 'banana', 'cherry', 'apple']
# Properties of lists
# - Ordered: Elements have a defined order.
# - Mutable: Elements can be changed.
# - Allows duplicates: Can contain duplicate elements.
# Common functions and methods for lists
fruits.append("orange") # Adds an element to the end of the list
print("After append:", fruits) # Output: ['apple', 'banana', 'cherry', 'apple', 'orange']
fruits.remove("banana") # Removes the first occurrence of the element
print("After remove:", fruits) # Output: ['apple', 'cherry', 'apple', 'orange']
fruits.sort() # Sorts the list in ascending order
print("After sort:", fruits) # Output: ['apple', 'apple', 'cherry', 'orange']
# Tuples in Python
# Tuples are ordered, immutable collections of items. They allow duplicate elements.
# Example of a tuple
coordinates = (10, 20, 30, 10)
print("Tuple:", coordinates) # Output: Tuple: (10, 20, 30, 10)
# Properties of tuples
# - Ordered: Elements have a defined order.
# - Immutable: Elements cannot be changed.
# - Allows duplicates: Can contain duplicate elements.
# Common functions and methods for tuples
print("Length of tuple:", len(coordinates)) # Output: 4
print("Index of 20:", coordinates.index(20)) # Output: 1
print("Count of 10:", coordinates.count(10)) # Output: 2
# Dictionaries in Python
# Dictionaries are unordered collections of key-value pairs. Keys must be unique.
# Example of a dictionary
student = {"name": "Alice", "age": 25, "grade": "A"}
print("Dictionary:", student) # Output: Dictionary: {'name': 'Alice', 'age': 25, 'grade': 'A'}
# Properties of dictionaries
# - Unordered: Elements do not have a defined order (ordered since Python 3.7).
# - Mutable: Elements can be changed.
# - Unique keys: Keys must be unique.
# Common functions and methods for dictionaries
student["age"] = 26 # Updates the value for the key 'age'
print("After update:", student) # Output: {'name': 'Alice', 'age': 26, 'grade': 'A'}
student["major"] = "Computer Science" # Adds a new key-value pair
print("After adding new key:", student) # Output: {'name': 'Alice', 'age': 26, 'grade': 'A', 'major': 'Computer Science'}
del student["grade"] # Deletes the key-value pair for 'grade'
print("After delete:", student) # Output: {'name': 'Alice', 'age': 26, 'major': 'Computer Science'}
# Sets in Python
# Sets are unordered collections of unique items.
# Example of a set
unique_numbers = {1, 2, 3, 4, 4}
print("Set:", unique_numbers) # Output: Set: {1, 2, 3, 4}
# Properties of sets
# - Unordered: Elements do not have a defined order.
# - Mutable: Elements can be changed.
# - Unique elements: Cannot contain duplicate elements.
# Common functions and methods for sets
unique_numbers.add(5) # Adds an element to the set
print("After add:", unique_numbers) # Output: {1, 2, 3, 4, 5}
unique_numbers.remove(3) # Removes an element from the set
print("After remove:", unique_numbers) # Output: {1, 2, 4, 5}
another_set = {4, 5, 6, 7}
union_set = unique_numbers.union(another_set) # Returns the union of two sets
print("Union of sets:", union_set) # Output: {1, 2, 4, 5, 6, 7}