diff --git a/docs/01_Python_Introduction.pdf b/docs/01_Python_Introduction.pdf index 26ac32f..7d1f1ad 100644 Binary files a/docs/01_Python_Introduction.pdf and b/docs/01_Python_Introduction.pdf differ diff --git a/scripts/02_print.py b/scripts/02_print.py index 131d411..fd8456d 100644 --- a/scripts/02_print.py +++ b/scripts/02_print.py @@ -54,8 +54,9 @@ # \t - Tab print("Message1\tMessage2") -# ----------------------------------------------- -# CHALLENGE TIME +# --------------------------------------- +# Python Challenge +# --------------------------------------- # Recreate the following using ONLY ONE print() function: # Your learning Path: # -Python Basics diff --git a/scripts/03_variables.py b/scripts/03_variables.py new file mode 100644 index 0000000..3169d2c --- /dev/null +++ b/scripts/03_variables.py @@ -0,0 +1,51 @@ +# ================================================================================ +# VARIABLES +# ---------------------------------------- +# Variables act as named containers for storing data in Python. +# You give a variable a value using the assignment operator `=`, then +# you can reuse that name throughout your code. We’ll also use +# the `print()` function to display both literals and variables. +# ================================================================================ + +# --------------------------------------- +# Without Variables +# --------------------------------------- +# Here we print fixed text directly using print(). +print("My name is Baraa") +print("Baraa is learning Python") +print("Baraa wants to become Python expert") + + +# --------------------------------------- +# Single Variable +# --------------------------------------- +# Store your name in a variable and reuse it in print(). +name = "Baraa" +print("My name is", name) +print(name, "is learning Python") +print(name, "wants to become Python expert") + + +# --------------------------------------- +# Multiple Variables +# --------------------------------------- +# Now we’ll keep both your name and the language in variables. +name = "Baraa" +language = "python" +print("My name is", name) +print(name, "is learning", language) +print(name, "wants to become", language, "expert") + +# --------------------------------------- +# Python Challenge +# --------------------------------------- +# Print the following three lines: +# info@datawithbaraa.com +# support@datawithbaraa.com +# www.datawithbaraa.com +# Use a variable for the base domain to make it dynamic! + +domain = "datawithbaraa.com" +print("info@" + domain) +print("support@" + domain) +print("www." + domain) diff --git a/scripts/04_input.py b/scripts/04_input.py new file mode 100644 index 0000000..a80f363 --- /dev/null +++ b/scripts/04_input.py @@ -0,0 +1,16 @@ +# ================================================================================ +# VARIABLES & USER INPUT +# ---------------------------------------- +# This section shows how to accept user input using the `input()` function. +# We then combine it with a predefined variable to display a complete sentence. +# ================================================================================ + + +# Dynamic Value: Ask the user to enter their name and store it in a variable. +name = input("Enter Your Name:") + +# Hardcoded value +country = "Germany" + +# Combine both variables +print(name, "comes from", country) diff --git a/scripts/05_data_types.py b/scripts/05_data_types.py new file mode 100644 index 0000000..8eb571b --- /dev/null +++ b/scripts/05_data_types.py @@ -0,0 +1,53 @@ +# ================================================================================ +# DATA TYPES +# ---------------------------------------- +# Python has multiple built-in data types to represent different kinds of values. +# Common types include integers, floats, strings, booleans, and NoneType. +# ================================================================================ + +# --------------------------------------- +# Examples of Different Data Types +# --------------------------------------- +a = 10 # int +b = 3.15 # float +c = "Hello" # str (double quotes) +d = 'Hi' # str (single quotes) +e = "1234" # str (looks like a number, but it's a string) +f = True # bool +g = False # bool +h = None # NoneType +i = "" # str - empty string +j = " " # str - contains a single space + +# --------------------------------------- +# Using type() to Check Data Types +# --------------------------------------- +text = "hi" +number = 10 + +print(type(text)) # ➜ +print(type(number)) # ➜ + +# --------------------------------------- +# Exploring Methods for Each Data Type +# --------------------------------------- +# Some methods are specific to certain types. +print(text.upper()) # "HI" (string method) +print(number.bit_length()) # 4 (integer method) +# print(text.bit_length()) # Error: str has no bit_length() + +# --------------------------------------- +# Python Challenge +# --------------------------------------- +# Create 5 variables, each with a different data type: +# - Your age +# - Your height (with decimals) +# - Your name +# - Are you a student? +# - Something with no value yet + +age = 30 # int +height = 1.75 # float +name = "Maria" # str +is_student = False # bool +has_kids = None # NoneType diff --git a/scripts/06_string_functions.py b/scripts/06_string_functions.py new file mode 100644 index 0000000..cd66a3f --- /dev/null +++ b/scripts/06_string_functions.py @@ -0,0 +1,195 @@ +# ================================================================================ +# STRING FUNCTIONS +# ---------------------------------------- +# Strings are one of the most used data types in Python. +# This section covers type conversion, transformations, formatting, +# indexing, slicing, cleanup, and search operations on strings. +# ================================================================================ + +# --------------------------------------- +# Type Conversion: Numbers to Strings +# --------------------------------------- +name = "Baraa" +print(type(name)) # + +age = 24 +print(type(age)) # +print("Your Age is: " + str(age)) # Must convert int to str for concatenation + +age = age + 5 # ➜ 29 (int) +age = str(age) # Convert to string +print(type(age)) # ➜ + +# age = age + 5 # Error: Cannot add int to str + +# --------------------------------------- +# String Length +# --------------------------------------- +password = "123a58478as" +print(len(password)) # ➜ 11 + +if len(password) < 8: + print("Your Password is too short!") + +# --------------------------------------- +# Counting Substrings +# --------------------------------------- +text = """ +Python is easy to learn. +Python is powerful$. +Many people love python. +""" + +print(text.count("Python")) # ➜ 2 (case-sensitive) +print(text.count("python")) # ➜ 1 +print(text.count("$")) # ➜ 1 + +# --------------------------------------- +# Replacing Characters +# --------------------------------------- +price = "1234,56" +print(price.replace(",", ".")) # ➜ 1234.56 + +phone = "176-1234-56" +print(phone.replace("-", "/")) # ➜ 176/1234/56 +print(phone.replace("-", "")) # ➜ 176123456 + +# --------------------------------------- +# Phone Number Cleanup Challenge +# --------------------------------------- +# Convert the messy phone number into a clean number format with only digits: +# Input: "+49 (176) 123-4567" +# Output: "00491761234567" + +raw_number = "+49 (176) 123-4567" +clean_number = raw_number.replace("+49", "0049").replace("(", "").replace(")", "").replace("-", "").replace(" ", "") +print(clean_number) # ➜ 00491761234567 + +# --------------------------------------- +# Combining Strings +# --------------------------------------- +first_name = "Michael" +last_name = "Scott" +full_name = first_name + "-" + last_name +print(full_name) # ➜ Michael-Scott + +folder = "C:/Users/Baraa/" +file = "report.csv" +full_file_path = folder + file +print(full_file_path) # ➜ C:/Users/Baraa/report.csv + +# --------------------------------------- +# String Formatting +# --------------------------------------- +name = "Sam" +age = 34 +is_student = False + +# Method 1: String Concatenation +print("My name is " + name + ", I am " + str(age) + " years old, and student status is " + str(is_student) + ".") + +# Method 2: f-Strings (Recommended) +print(f"My name is {name}, I am {age} years old, and student status is {is_student}.") + +# f-String Expression and Escape Example +print(f"2 + 3 = {2 + 3}") # ➜ 2 + 3 = 5 +print(f"{{This is me}}") # ➜ {This is me} + +# --------------------------------------- +# Splitting Strings +# --------------------------------------- +stamp = "2026-09-20 14:30" +print(stamp.split(" ")) # ➜ ['2026-09-20', '14:30'] + +csv_file = "1234,Max,USA,1970-10-05,M" +print(csv_file.split(",")) # ➜ ['1234', 'Max', 'USA', '1970-10-05', 'M'] + +# --------------------------------------- +# Repeating Strings +# --------------------------------------- +print("ha" * 3) # ➜ hahaha +print("=" * 30) # ➜ ============================== + +# --------------------------------------- +# Indexing and Slicing +# --------------------------------------- +text = "Python" + +print(text[0]) # ➜ P (first character) +print(text[-6]) # ➜ P (same as above) +print(text[5]) # ➜ n (last character) +print(text[-1]) # ➜ n +print(text[3]) # ➜ h + +date = "2026-09-20" +print(date[0:4]) # ➜ 2026 (year) +print(date[:4]) # ➜ 2026 +print(date[5:7]) # ➜ 09 (month) +print(date[8:]) # ➜ 20 (day) +print(date[-2:]) # ➜ 20 + +# --------------------------------------- +# Whitespace Cleanup +# --------------------------------------- +text = " Engineering".lstrip() +print(text) # ➜ "Engineering" + +text = "Engineering ".rstrip() +print(text) # ➜ "Engineering" + +text = " Engineering ".strip() +print(text) # ➜ "Engineering" + +text = "Data Engineering".strip() +print(text) # ➜ "Data Engineering" + +text = "###Abc###".strip("#") +print(text) # ➜ "Abc" + +# --------------------------------------- +# Case Conversion +# --------------------------------------- +text = "python PROGRAMMING" +print(text.lower()) # ➜ python programming +print(text.upper()) # ➜ PYTHON PROGRAMMING + +search = "Email ".lower().strip() +data = " emAil".lower().strip() +print(search == data) # ➜ True + +# --------------------------------------- +# Search Functions +# --------------------------------------- +phone = "+48-176-12345" +print(phone.startswith("+49")) # ➜ False + +email = "baraa@outlook.com" +print(email.endswith("gmail.com")) # ➜ False + +file = "data_backup.csv" +print(file.endswith(".csv")) # ➜ True + +print("@" in email) # ➜ True + +url = "https://api.company.com/v1/data" +print("/api" in url) # ➜ True + +# --------------------------------------- +# Partial Extraction Using find() +# --------------------------------------- +phone1 = "+48-176-12345" +phone2 = "48-654-16548" +phone3 = "0048-654-16548" + +print(phone1[phone1.find("-") + 1:]) # ➜ 176-12345 +print(phone2[phone2.find("-") + 1:]) # ➜ 654-16548 +print(phone3[phone3.find("-") + 1:]) # ➜ 654-16548 + +print(phone1.find("-")) # ➜ 3 + + +# --------------------------------------- +# String Cleanup Challenge +# --------------------------------------- +# Input: "968-Maria, ( D@ta Engineer );; 27y" +# Goal: name: maria | role: data engineer | age: 27 diff --git a/scripts/07_number_functions.py b/scripts/07_number_functions.py new file mode 100644 index 0000000..533bd57 --- /dev/null +++ b/scripts/07_number_functions.py @@ -0,0 +1,115 @@ +# ================================================================================ +# NUMERIC DATA TYPES +# ---------------------------------------- +# Python supports different numeric types: int, float, complex. +# You can also convert between types and perform arithmetic operations. +# ================================================================================ + +# --------------------------------------- +# Numeric Types +# --------------------------------------- +x = 5 +y = 5.7 +z = 2 + 3j + +print(type(x)) # ➜ +print(type(y)) # ➜ +print(type(z)) # ➜ + +# --------------------------------------- +# Type Conversion +# --------------------------------------- +x = "24" +print(type(x)) # ➜ + +x = int(x) +print(type(x)) # ➜ +print(x * 3) # ➜ 72 + +x = 3.14 +print(int(x)) # ➜ 3 + +x = 3 +print(float(x)) # ➜ 3.0 + +x = 3 # real part +y = 4 # imaginary part +print(complex(x, y)) # ➜ (3+4j) + +# --------------------------------------- +# Basic Arithmetic Operations +# --------------------------------------- +print(2 + 3) # ➜ 5 (Addition) +print(5 - 3) # ➜ 2 (Subtraction) +print(4 * 2) # ➜ 8 (Multiplication) +print(7 / 2) # ➜ 3.5 (True division → float) +print(7 // 2) # ➜ 3 (Floor division → int) +print(9 % 2) # ➜ 1 (Modulus → remainder) +print(2 ** 3) # ➜ 8 (Exponentiation) + + +# --------------------------------------- +# Compound Assignment Operators +# --------------------------------------- +x = 2 # Step 1: Initialize x with value 2 + +x += 3 # Step 2: x = x + 3 → x becomes 5 +print(x) # ➜ 5 + +x -= 1 # Step 3: x = x - 1 → x becomes 4 +print(x) # ➜ 4 + +x *= 2 # Step 4: x = x * 2 → x becomes 8 +print(x) # ➜ 8 + + +# --------------------------------------- +# Absolute Value +# --------------------------------------- +print(abs(2 - 10)) # ➜ 8 + +# --------------------------------------- +# Rounding and Math Module +# --------------------------------------- +import math + +price = 35.54879865 + +print(round(price)) # ➜ 36 (nearest whole number) +print(round(price, 2)) # ➜ 35.55 (2 decimal places) +print(round(price, 1)) # ➜ 35.5 + +print(math.floor(price)) # ➜ 35 (round down) +print(math.ceil(price)) # ➜ 36 (round up) +print(math.trunc(price)) # ➜ 35 (truncate decimal part) +print(int(price)) # ➜ 35 (same as trunc) + +# --------------------------------------- +# Random Numbers +# --------------------------------------- +import random + +print(random.random()) # ➜ Random float between 0 and 1 +print(random.randint(1, 6)) # ➜ Simulates a dice roll (1 to 6) + +# --------------------------------------- +# Checking Integer Values +# --------------------------------------- +x = 7.0 +print(x.is_integer()) # ➜ True → 7.0 is float but represents an int + +y = 7.1 +print(y.is_integer()) # ➜ False → not a whole number + +# --------------------------------------- +# Type Checking +# --------------------------------------- +x = 70.4 +print(isinstance(x, int)) # ➜ False +print(isinstance(x, float)) # ➜ True + +# --------------------------------------- +# Python Challenge +# --------------------------------------- +# Generate a random integer between 1 and 100, +# then check if the result is an even number. diff --git a/scripts/08_boolean_functions.py b/scripts/08_boolean_functions.py new file mode 100644 index 0000000..d11b2d6 --- /dev/null +++ b/scripts/08_boolean_functions.py @@ -0,0 +1,49 @@ +# ================================================================================ +# BOOLEAN VALUES +# ---------------------------------------- +# Booleans are either True or False. They're often used for logic, conditions, +# validations, and control flow. Python also allows checking "truthiness" of values. +# ================================================================================ + +# --------------------------------------- +# Basic Boolean Values +# --------------------------------------- +print(True) # ➜ True +print(False) # ➜ False +print(type(True)) # ➜ + +# --------------------------------------- +# bool() Function: Truthiness of Values +# --------------------------------------- +print(bool(123)) # ➜ True (non-zero number) +print(bool("Hi")) # ➜ True (non-empty string) + +print(bool(())) # ➜ False (empty tuple) +print(bool(0)) # ➜ False (zero) +print(bool("")) # ➜ False (empty string) +print(bool(None)) # ➜ False (None is always considered False) + +# --------------------------------------- +# Using any() and all() for Field Validation +# --------------------------------------- +email = "" +phone = "017-1234" +username = "baraa123" + +# Allows registration if at least one field is filled +print(any([email, phone, username])) # ➜ True + +# Allows registration only if all fields are filled +print(all([email, phone, username])) # ➜ False + +# --------------------------------------- +# Type Checking with isinstance() +# --------------------------------------- +print(isinstance(123, int)) # ➜ True +print(isinstance(True, str)) # ➜ False + +# --------------------------------------- +# String Start/End Checks +# --------------------------------------- +print("Hello".endswith("o")) # ➜ True +print("Hello".startswith("o")) # ➜ False diff --git a/scripts/09_comparison_operators.py b/scripts/09_comparison_operators.py new file mode 100644 index 0000000..efe5138 --- /dev/null +++ b/scripts/09_comparison_operators.py @@ -0,0 +1,49 @@ +# ================================================================================ +# COMPARISON OPERATORS +# ---------------------------------------- +# Comparison operators are used to compare values and return Boolean results. +# These include equality, inequality, greater/less than, and range comparisons. +# ================================================================================ + +# --------------------------------------- +# Basic Comparison Operators +# --------------------------------------- +print(10 == 10) # ➜ True (equal to) +print(10 != 10) # ➜ False (not equal to) +print(7 > 3) # ➜ True (greater than) +print(7 >= 3) # ➜ True (greater than or equal to) +print(3 < 7) # ➜ True (less than) +print(7 <= 7) # ➜ True (less than or equal to) + +# --------------------------------------- +# String Comparison: Case Sensitivity +# --------------------------------------- +print("a" == "A") # ➜ False (case-sensitive) +print("a".lower() == "A".lower()) # ➜ True (case-insensitive comparison) + +# --------------------------------------- +# Alphabetical Comparison +# --------------------------------------- +print("a" < "b") # ➜ True — "a" comes before "b" in ASCII order + +# --------------------------------------- +# Common Mistake: Using = Instead of == +# --------------------------------------- +# print("a" = "A") # SyntaxError: cannot use = in comparisons +# Correct usage: +print("a" == "A") # ➜ False + +# --------------------------------------- +# Assignment vs Comparison +# --------------------------------------- +x = "a" # Assignment +print(x == "a") # ➜ True (comparison) + +# --------------------------------------- +# Chained Comparison (Range Check) +# --------------------------------------- +age = 18 +print(18 <= age <= 30) # ➜ True (age is between 18 and 30) + +age = 35 +print(18 <= age <= 30) # ➜ False (35 is outside the range) diff --git a/scripts/10_logical_operators.py b/scripts/10_logical_operators.py new file mode 100644 index 0000000..9ed12ff --- /dev/null +++ b/scripts/10_logical_operators.py @@ -0,0 +1,106 @@ +# ================================================================================ +# LOGICAL OPERATORS & IDENTITY CHECKS +# ---------------------------------------- +# Python provides logical operators to combine multiple conditions: +# `and`, `or`, `not` — which return Boolean results based on truth tables. +# This section also covers `in`, `not in`, `is`, and `is not`. +# ================================================================================ + +# --------------------------------------- +# Basic Logical Operators: and / or +# --------------------------------------- +print(3 > 1 and 5 < 1) # ➜ False: first is True, second is False +print(3 > 1 and 5 > 1) # ➜ True: both are True + +print(3 > 1 or 5 < 1) # ➜ True: first is True, second is False +print(3 > 1 or 5 > 1) # ➜ True: both are True + +# --------------------------------------- +# Real-World Example: System Monitoring +# --------------------------------------- +cpu_usage = 70 +memory_usage = 90 + +print(cpu_usage > 90 or memory_usage > 90) # ➜ False: both values are within limits + +# --------------------------------------- +# Login Validation +# --------------------------------------- +email = True +password = False + +print(email and password) # ➜ False: both must be True to allow login + +# --------------------------------------- +# Logical NOT +# --------------------------------------- +print(not 3 > 2) # ➜ False +print(not True) # ➜ False +print(not False) # ➜ True +print(not not False) # ➜ False + +name = "" +print(not name) # ➜ True: empty string is falsy +print(not 10) # ➜ False: 10 is truthy + +# --------------------------------------- +# Complex Access Control Logic +# --------------------------------------- +# Allow access if the user is logged in OR a guest, +# BUT they must not be banned. + +is_logged_in = True +is_guest = False +is_banned = True + +print(is_logged_in or is_guest and not is_banned) # ➜ True (wrong logic) +print((is_logged_in or is_guest) and not is_banned) # ➜ False (correct logic) + +# --------------------------------------- +# Python Challenges +# --------------------------------------- +# 1. Name is not empty and age is >= 18 +# 2. Password is at least 8 chars and has no spaces +# 3. Email is not empty, contains '@', and ends with '.com' +# 4. Username is a string, not None, and longer than 5 characters +# 5. User is admin or moderator, and not banned or email verified + +# --------------------------------------- +# Membership Operators: in / not in +# --------------------------------------- +print("f" not in "python") # ➜ True +print(3 not in [1, 2, 3]) # ➜ False + +domain = "spam.com" +banned_domains = ["spam.com", "fake.org", "bot.net"] +print(domain not in banned_domains) # ➜ False + +# --------------------------------------- +# Identity Operators: is / is not +# --------------------------------------- +x = ['a', 'b', 'c'] +y = ['a', 'b', 'c'] + +print(x == y) # ➜ True: same content +print(x is y) # ➜ False: different objects in memory + +x = 10 +y = 10 + +print(x == y) # ➜ True: same value +print(x is y) # ➜ True: same object (small integers are cached) + +x = ['a', 'b', 'c'] +y = x + +print(x == y) # ➜ True: same values +print(x is y) # ➜ True: same object (same memory reference) + +# --------------------------------------- +# Validate Email Exists and Is Not Empty +# --------------------------------------- +email = "b@gmail.com" +print(email != "") # ➜ True + +email = None +print(email is not None and email != "") # ➜ False diff --git a/scripts/11_conditional_statements.py b/scripts/11_conditional_statements.py new file mode 100644 index 0000000..fd62b69 --- /dev/null +++ b/scripts/11_conditional_statements.py @@ -0,0 +1,103 @@ +# ================================================================================ +# CONDITIONAL STATEMENTS (IF / ELSE / ELIF) +# ---------------------------------------- +# Python uses `if`, `elif`, and `else` to control the flow of decisions. +# You can nest conditions or combine them using logical operators. +# ================================================================================ + +# --------------------------------------- +# Simple If Condition +# --------------------------------------- +score = 100 +if score >= 90: + print("A") + +# --------------------------------------- +# If / Else +# --------------------------------------- +score = 50 +if score >= 90: + print("A") +else: + print("F") + +# --------------------------------------- +# If / Elif / Else +# --------------------------------------- +score = 50 +if score >= 90: + print("A") +elif score >= 80: + print("B") +else: + print("F") + +# --------------------------------------- +# Full Grading Logic with Multiple Elif +# --------------------------------------- +score = 50 +if score >= 90: + print("A") +elif score >= 80: + print("B") +elif score >= 70: + print("C") +elif score >= 60: + print("D") +else: + print("F") + +# --------------------------------------- +# Nested If: Project Bonus +# --------------------------------------- +score = 50 +submitted_project = True + +if score >= 90: + if submitted_project: + print("A+") + else: + print("A") +elif score >= 80: + print("B") +elif score >= 70: + print("C") +elif score >= 60: + print("D") +else: + print("F") + +# --------------------------------------- +# Combined Conditions with and/or +# --------------------------------------- +score = 50 +submitted_project = True + +if score >= 90 and submitted_project: + print("A+") +elif score >= 90: + print("A") +elif score >= 80: + print("B") +elif score >= 70: + print("C") +elif score >= 60 or submitted_project: + print("D") +else: + print("F") + +# --------------------------------------- +# Independent Conditions +# --------------------------------------- +score = 50 +submitted_project = True + +if score >= 90: + print("High Score") +else: + print("Low Score") + +if submitted_project: + print("Project is submitted") +else: + print("Project is not submitted")