Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions 01_Variables_and_Data_Types/01_basic_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Basic Variables in Python
==========================
This program demonstrates how to create and use variables in Python.
Variables are containers for storing data values.
"""

# Creating variables with different data types
name = "Alice" # String variable
age = 20 # Integer variable
height = 5.6 # Float variable
is_student = True # Boolean variable

# Printing variables
print("=== Student Information ===")
print("Name:", name)
print("Age:", age)
print("Height:", height, "feet")
print("Is Student:", is_student)

# You can change variable values
print("\n=== After one year ===")
age = age + 1 # Increasing age by 1
print("Age:", age)

# Multiple variables in one line
x, y, z = 10, 20, 30
print("\n=== Multiple Variables ===")
print("x =", x)
print("y =", y)
print("z =", z)

# Same value to multiple variables
a = b = c = 100
print("\n=== Same Value to Multiple Variables ===")
print("a =", a)
print("b =", b)
print("c =", c)

# Variable naming rules demonstration
student_name = "Bob" # Good: descriptive and uses snake_case
_private_var = 42 # Good: starts with underscore (convention for private)
# 2nd_student = "Charlie" # Bad: cannot start with number (commented out)

print("\n=== Variable Naming ===")
print("Student Name:", student_name)
print("Private Variable:", _private_var)
56 changes: 56 additions & 0 deletions 01_Variables_and_Data_Types/02_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Working with Numbers in Python
===============================
This program demonstrates integer and float operations.
"""

# Integer examples
num1 = 10
num2 = 3

print("=== Integer Operations ===")
print("Number 1:", num1)
print("Number 2:", num2)
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2) # Returns float
print("Floor Division:", num1 // num2) # Returns integer (rounds down)
print("Modulus (Remainder):", num1 % num2)
print("Power:", num1 ** num2) # 10 to the power of 3

# Float examples
price1 = 19.99
price2 = 5.50

print("\n=== Float Operations ===")
print("Price 1: $", price1)
print("Price 2: $", price2)
print("Total Price: $", price1 + price2)
print("Average Price: $", (price1 + price2) / 2)

# Mixed operations (int and float)
quantity = 5 # Integer
price_per_item = 12.50 # Float
total = quantity * price_per_item

print("\n=== Shopping Bill ===")
print("Quantity:", quantity)
print("Price per item: $", price_per_item)
print("Total amount: $", total)

# Useful number functions
number = -42.7

print("\n=== Number Functions ===")
print("Original number:", number)
print("Absolute value:", abs(number)) # Removes negative sign
print("Rounded value:", round(number)) # Rounds to nearest integer
print("Rounded to 1 decimal:", round(number, 1))

# Finding max and min
a, b, c = 15, 28, 9
print("\n=== Max and Min ===")
print("Numbers:", a, b, c)
print("Maximum:", max(a, b, c))
print("Minimum:", min(a, b, c))
85 changes: 85 additions & 0 deletions 01_Variables_and_Data_Types/03_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Working with Strings in Python
===============================
This program demonstrates string operations and methods.
"""

# Creating strings
name = "Alice"
message = 'Hello World'
multiline = """This is a
multi-line
string"""

print("=== Basic Strings ===")
print("Name:", name)
print("Message:", message)
print("Multiline String:")
print(multiline)

# String concatenation (joining)
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name

print("\n=== String Concatenation ===")
print("First Name:", first_name)
print("Last Name:", last_name)
print("Full Name:", full_name)

# String repetition
laugh = "ha"
print("\n=== String Repetition ===")
print("Laugh:", laugh * 5) # Prints "hahahahaha"

# String length
sentence = "Python is fun"
print("\n=== String Length ===")
print("Sentence:", sentence)
print("Length:", len(sentence))

# Accessing characters (indexing)
word = "Python"
print("\n=== String Indexing ===")
print("Word:", word)
print("First character:", word[0]) # P
print("Second character:", word[1]) # y
print("Last character:", word[-1]) # n
print("Second last:", word[-2]) # o

# String slicing
text = "Hello World"
print("\n=== String Slicing ===")
print("Original text:", text)
print("First 5 characters:", text[0:5]) # Hello
print("From index 6 to end:", text[6:]) # World
print("Last 5 characters:", text[-5:]) # World

# Useful string methods
sample = " python programming "
print("\n=== String Methods ===")
print("Original:", "[" + sample + "]")
print("Uppercase:", sample.upper())
print("Lowercase:", sample.lower())
print("Capitalized:", sample.capitalize())
print("Stripped (remove spaces):", "[" + sample.strip() + "]")
print("Replace:", sample.replace("python", "Python"))

# Checking string content
email = "student@example.com"
print("\n=== String Checks ===")
print("Email:", email)
print("Contains '@':", "@" in email)
print("Starts with 'student':", email.startswith("student"))
print("Ends with '.com':", email.endswith(".com"))

# String formatting
name = "Alice"
age = 20
print("\n=== String Formatting ===")
# Method 1: f-strings (recommended)
print(f"My name is {name} and I am {age} years old.")
# Method 2: format() method
print("My name is {} and I am {} years old.".format(name, age))
# Method 3: % operator (old style)
print("My name is %s and I am %d years old." % (name, age))
88 changes: 88 additions & 0 deletions 01_Variables_and_Data_Types/04_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
Working with Lists in Python
=============================
This program demonstrates list operations and methods.
Lists are ordered, mutable collections that can contain different data types.
"""

# Creating lists
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
empty_list = []

print("=== Creating Lists ===")
print("Fruits:", fruits)
print("Numbers:", numbers)
print("Mixed types:", mixed)
print("Empty list:", empty_list)

# Accessing list elements
print("\n=== Accessing Elements ===")
print("First fruit:", fruits[0])
print("Second fruit:", fruits[1])
print("Last fruit:", fruits[-1])

# List length
print("\n=== List Length ===")
print("Number of fruits:", len(fruits))

# Adding elements to list
fruits.append("grape") # Add at the end
print("\n=== Adding Elements ===")
print("After append:", fruits)

fruits.insert(1, "mango") # Insert at specific position
print("After insert at position 1:", fruits)

# Removing elements from list
fruits.remove("banana") # Remove specific item
print("\n=== Removing Elements ===")
print("After removing banana:", fruits)

last_fruit = fruits.pop() # Remove and return last item
print("Popped item:", last_fruit)
print("After pop:", fruits)

# List slicing
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print("\n=== List Slicing ===")
print("Original list:", numbers)
print("First 5 elements:", numbers[0:5])
print("Elements from index 5:", numbers[5:])
print("Last 3 elements:", numbers[-3:])
print("Every second element:", numbers[::2])

# List operations
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print("\n=== List Operations ===")
print("List 1:", list1)
print("List 2:", list2)
print("Concatenation:", list1 + list2)
print("Repetition:", list1 * 3)

# Useful list methods
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print("\n=== List Methods ===")
print("Original list:", numbers)
print("Count of 1:", numbers.count(1))
print("Index of 4:", numbers.index(4))

numbers.sort() # Sort in ascending order
print("Sorted list:", numbers)

numbers.reverse() # Reverse the list
print("Reversed list:", numbers)

# Checking if item exists in list
shopping_list = ["milk", "bread", "eggs", "butter"]
print("\n=== Checking Items ===")
print("Shopping list:", shopping_list)
print("Is 'milk' in list?", "milk" in shopping_list)
print("Is 'cheese' in list?", "cheese" in shopping_list)

# List comprehension (advanced but useful)
squares = [x**2 for x in range(1, 6)]
print("\n=== List Comprehension ===")
print("Squares of 1-5:", squares)
96 changes: 96 additions & 0 deletions 01_Variables_and_Data_Types/05_tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
Working with Tuples in Python
==============================
This program demonstrates tuple operations.
Tuples are ordered, immutable collections (cannot be changed after creation).
"""

# Creating tuples
coordinates = (10, 20)
rgb_color = (255, 0, 0)
mixed_tuple = (1, "hello", 3.14, True)
single_item = (42,) # Note: comma is required for single item
empty_tuple = ()

print("=== Creating Tuples ===")
print("Coordinates:", coordinates)
print("RGB Color:", rgb_color)
print("Mixed tuple:", mixed_tuple)
print("Single item tuple:", single_item)
print("Empty tuple:", empty_tuple)

# Accessing tuple elements
point = (100, 200, 300)
print("\n=== Accessing Elements ===")
print("Point:", point)
print("X coordinate:", point[0])
print("Y coordinate:", point[1])
print("Z coordinate:", point[2])
print("Last element:", point[-1])

# Tuple length
print("\n=== Tuple Length ===")
print("Length of point:", len(point))

# Tuple unpacking
x, y, z = point
print("\n=== Tuple Unpacking ===")
print("x =", x)
print("y =", y)
print("z =", z)

# Why use tuples? They are immutable
print("\n=== Immutability Demo ===")
print("Original coordinates:", coordinates)
# coordinates[0] = 30 # This would cause an error!
print("Tuples cannot be changed after creation")

# Tuple methods (only 2 methods since tuples are immutable)
numbers = (1, 2, 3, 2, 4, 2, 5)
print("\n=== Tuple Methods ===")
print("Numbers:", numbers)
print("Count of 2:", numbers.count(2))
print("Index of 4:", numbers.index(4))

# Tuple operations
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print("\n=== Tuple Operations ===")
print("Tuple 1:", tuple1)
print("Tuple 2:", tuple2)
print("Concatenation:", tuple1 + tuple2)
print("Repetition:", tuple1 * 3)

# Converting between lists and tuples
my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(my_list)
back_to_list = list(my_tuple)

print("\n=== List and Tuple Conversion ===")
print("Original list:", my_list)
print("Converted to tuple:", my_tuple)
print("Back to list:", back_to_list)

# Practical use case: returning multiple values from a function
def get_student_info():
"""Returns student information as a tuple"""
name = "Alice"
age = 20
grade = "A"
return (name, age, grade) # Or just: return name, age, grade

print("\n=== Practical Use Case ===")
student_data = get_student_info()
print("Student data (tuple):", student_data)

# Unpacking the returned tuple
student_name, student_age, student_grade = get_student_info()
print(f"Name: {student_name}, Age: {student_age}, Grade: {student_grade}")

# When to use tuples vs lists?
print("\n=== When to Use Tuples ===")
print("Use tuples when:")
print(" 1. Data should not change (coordinates, RGB values)")
print(" 2. Returning multiple values from a function")
print(" 3. Using as dictionary keys (lists can't be keys)")
print(" 4. Slightly faster than lists")
Loading