## 🐍 Python Comprehensive Guide: Core Concepts
# 1. Fundamental Data Types
# Integer (int): Whole numbers
x = 100
# Float (float): Decimal numbers
y = 3.14159
# String (str): Text data enclosed in quotes
name = "Jane Doe"
# Boolean (bool): True or False
is_active = True
# Type Casting
int_from_float = int(y) # Result: 3
float_from_int = float(x) # Result: 100.0
# 2. String Manipulation
s = " pyTHon pRoGramming "
print([Link]()) # ' python programming '
print([Link]()) # ' PYTHON PROGRAMMING '
print([Link]()) # 'pyTHon pRoGramming' (Removes leading/trailing whitespace)
print([Link]('o', '0')) # ' pyTH0n pR0Gramming '
print([Link](" ")) # True
print(len(s)) # Length of the string
# String Formatting (f-strings - preferred method)
age = 30
message = f"Hello, {name}. You are {age} years old."
print(message)
# 3. Arithmetic and Comparison Operators
a, b = 15, 4
print(a + b) # Addition (19)
print(a % b) # Modulus/Remainder (3)
print(a // b) # Floor Division (3)
print(a > b) # Comparison (True)
print(a == 15) # Equality Check (True)
print(a != b) # Inequality Check (True)
# 4. Control Flow: If/Elif/Else
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
print(f"Grade: {grade}")
# Logical Operators
is_sunny = True
is_warm = False
if is_sunny and not is_warm:
print("It's sunny but cool.")
# 5. Data Structures
# A. Lists (Mutable/Changeable, ordered)
my_list = [10, "apple", 5.5, 10]
my_list.append("banana")
my_list.insert(1, "orange")
my_list.pop() # Removes the last item ("banana")
my_list[0] = 100 # Change item at index 0
print(my_list[1]) # Access item: orange
print(my_list.count(10)) # Counts occurrences of 10 (1)
# B. Tuples (Immutable/Unchangeable, ordered)
my_tuple = (1, "a", 2.0)
# my_tuple[0] = 5 # ERROR: Cannot modify tuples
# C. Dictionaries (Mutable, Key:Value pairs, unordered in older Python versions)
person = {
"name": "Alex",
"age": 25,
"city": "London"
}
print(person["name"]) # Access value: Alex
person["age"] = 26 # Modify value
person["job"] = "Developer" # Add new key-value pair
print([Link]()) # Get all keys
print([Link]()) # Get all values
# 6. Loops
# For Loop (Iterating through sequences)
fruits = ["apple", "kiwi", "grape"]
for fruit in fruits:
print(f"I like {fruit}.")
# Using range()
for i in range(3): # range(start, stop, step) -> default: start=0, step=1
print(i) # Prints 0, 1, 2
# While Loop (Looping as long as a condition is True)
count = 0
while count < 4:
print("Count:", count)
count += 1 # Increment to prevent infinite loop
# Loop Control Statements
for num in range(10):
if num == 3:
continue # Skip the rest of the loop block and move to the next iteration
if num == 8:
break # Exit the loop entirely
print("Current num:", num) # 0, 1, 2, 4, 5, 6, 7
# 7. Functions
# Defining a function
def calculate_area(length, width):
"""Calculates the area of a rectangle."""
area = length * width
return area
# Calling the function
room_area = calculate_area(5, 10)
print(f"The area is: {room_area}")
# Function with default parameter value
def greet_user(name="Guest"):
print(f"Welcome, {name}!")
greet_user("Sarah") # Output: Welcome, Sarah!
greet_user() # Output: Welcome, Guest!
# 8. Input/Output (I/O)
# Taking input from the user (always returns a string)
# user_age = input("How old are you? ")
# print(f"You entered: {user_age}")