From 439bb6216819d31bb076ff99fe6fa2ee93b4536c Mon Sep 17 00:00:00 2001 From: MuhammadHuzaifaChaudhary Date: Tue, 14 Jul 2026 02:58:01 +0500 Subject: [PATCH] changing --- day 1.py | 9 ++- f_string.py | 18 +++++- for loop.py | 1 + func_argument.py | 46 ++++++++++++++ function_overloading.py | 7 ++- functions.py | 8 +++ hello.py | 1 + if else.py | 5 +- input.py | 15 ++++- lambda_function.py | 24 ++++++++ local_ global.py | 23 +++++++ nested_if.py | 120 +++++++++++++++++++++++++++++++++++++ strings,methods,slicing.py | 5 +- switch case.py | 3 +- while_loop.py | 2 + 15 files changed, 278 insertions(+), 9 deletions(-) diff --git a/day 1.py b/day 1.py index 6a42cbd..a327d83 100644 --- a/day 1.py +++ b/day 1.py @@ -6,11 +6,18 @@ # by just selecting all text and do ctrl + / # we can also do it by using triple quotes +"""hello how are you my gng +i am fine and you +i am also fine""" +# and these comments are multiple line comments + +# and if we store them in a variable then it become multiline string and we can print it apple="""hello how are you my gng i am fine and you i am also fine""" print(apple) -# and these comments are multiple line comments + + # now for printing double quotes we can do it by using single quotes #we cannot use double quotes inside double quotes diff --git a/f_string.py b/f_string.py index 794de52..9ae6767 100644 --- a/f_string.py +++ b/f_string.py @@ -1,3 +1,5 @@ +# f string is also used to format the string in python and +# it is more efficient than + operator to concatenate the string and variable. name = "Alice" age = 25 height = 1.75 @@ -36,4 +38,18 @@ # 8. Debugging (Python 3.8+) x = 10 -print(f"{x = }") # Prints: x = 10 \ No newline at end of file +print(f"{x = }") # Prints: x = 10 + + +# any operation can be done inside the f string like addition, subtraction, multiplication, division, etc. +# Anything inside {} is evaluated/executed +print(f"{2 + 2}") # 4 +print(f"{5 * 3}") # 15 +print(f"{10 / 2}") # 5.0 +print(f"{2 ** 10}") # 1024 + + +text = "hello world" +print(f"{text.replace('world', 'Python')}") # hello Python +print(f"{text.split()}") # ['hello', 'world'] +print(f"{text.upper().lower()}") # hello world \ No newline at end of file diff --git a/for loop.py b/for loop.py index 5a27b1e..8bc0abb 100644 --- a/for loop.py +++ b/for loop.py @@ -6,6 +6,7 @@ # in python # Python: Iterates over a range +# using range() function for i in range(5): print(i) # Output: 0 1 2 3 4 diff --git a/func_argument.py b/func_argument.py index e69de29..206824b 100644 --- a/func_argument.py +++ b/func_argument.py @@ -0,0 +1,46 @@ + + +def describe_pet(name, animal_type): + print(f"I have a {animal_type} named {name}") + +# Call with positional arguments +describe_pet("Max", "dog") # I have a dog named Max +describe_pet("Whiskers", "cat") # I have a cat named Whiskers + + + + +# call with default arguments +# Default parameters (C++ equivalent but more flexible) +def greet(name, greeting="Hello"): + print(f"{greeting}, {name}!") + +greet("Alice") # Hello, Alice! (uses default) +greet("Bob", "Hi") # Hi, Bob! +greet("Charlie", "Good morning") # Good morning, Charlie! + +# Multiple default parameters +def create_user(name, age=18, city="Unknown"): + print(f"User: {name}, Age: {age}, City: {city}") + +create_user("John") # User: John, Age: 18, City: Unknown +create_user("Sarah", 25) # User: Sarah, Age: 25, City: Unknown +create_user("Mike", 30, "New York") # User: Mike, Age: 30, City: New York + + + +# Keyword arguments make code more readable! +def describe_pet(name, animal_type, age): + print(f"{name} is a {animal_type} and is {age} years old") + +# Positional +describe_pet("Max", "dog", 3) + +# Keyword (order doesn't matter!) +describe_pet(animal_type="cat", name="Whiskers", age=5) +describe_pet(name="Buddy", age=2, animal_type="bird") + +# Mix positional and keyword (positional must come first!) +describe_pet("Max", animal_type="dog", age=3) # ✅ Correct +# describe_pet(name="Max", "dog", age=3) # ❌ SyntaxError! + diff --git a/function_overloading.py b/function_overloading.py index 03a0e85..a07c851 100644 --- a/function_overloading.py +++ b/function_overloading.py @@ -31,7 +31,10 @@ def add(a, b): print(add("Hello", "World")) # Hello World -# or by using single dispatch function + + +# or by using singled ispatch function + from functools import singledispatch @singledispatch @@ -54,3 +57,5 @@ def _(value): process("hello") # String: hello process([1, 2, 3]) # List with 3 items process(3.14) # Default: 3.14 + + diff --git a/functions.py b/functions.py index 17c5d56..2b9dcfb 100644 --- a/functions.py +++ b/functions.py @@ -44,3 +44,11 @@ def get_stats(numbers): minimum, maximum, average = get_stats([1, 2, 3, 4, 5]) print(f"Min: {minimum}, Max: {maximum}, Avg: {average:.2f}") + + +a=10 +b=9 +if(a>b): + pass +# this pass will say python to do nothing and +# continue with the rest of the code. \ No newline at end of file diff --git a/hello.py b/hello.py index e69de29..e75154b 100644 --- a/hello.py +++ b/hello.py @@ -0,0 +1 @@ +print("hello world") \ No newline at end of file diff --git a/if else.py b/if else.py index 7b0cb27..3e1fbec 100644 --- a/if else.py +++ b/if else.py @@ -32,7 +32,7 @@ # python syntax x=9; -if 0 < x < 10: # Python allows this! +if (0 < x < 10): # Python allows this! print("x is between 0 and 10") # ternary operator in c++ @@ -45,4 +45,5 @@ # if x > 5: # print("Hello") # ❌ IndentationError -# print("World") # ❌ Mixed indentation \ No newline at end of file +# print("World") # ❌ Mixed indentation + diff --git a/input.py b/input.py index b5ea56d..d08e71f 100644 --- a/input.py +++ b/input.py @@ -6,10 +6,23 @@ print("enter your age: ") age=input() print("You are " + age + " years old.") +#as age is string here suppose we take it in +# int(input(enetr your age:)) then we we will use commas instead +# of + to print the age because we cannot concatenate +# string and integer. + +# if you want to use + then you have to do str(age) means +# again convert it into string to print +# because + can only concatenate string and string not +# string and integer. + # here + is used to concatenate the string and variable. + # python input is always a string, # so if we want to take integer input we have to convert it # into integer using int() function. gpa = int(input("Enter your gpa: ")) cgpa = int(input("Enter your cgpa: ")) -print(gpa+cgpa) \ No newline at end of file +print(gpa+cgpa) +# we can also do print(int(gpa)+int(cgpa)) +# if gpa and cgpa are string type but it is not a good practice diff --git a/lambda_function.py b/lambda_function.py index e69de29..83d806a 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -0,0 +1,24 @@ +# lambda then variable name then : then expression + +# Simple lambda +add = lambda a, b: a + b +print(add(5, 3)) # 8 + +# Lambda with one parameter +square = lambda x: x ** 2 +print(square(5)) # 25 + +# Lambda as function argument +numbers = [1, 2, 3, 4, 5] +squared = list(map(lambda x: x ** 2, numbers)) +print(squared) # [1, 4, 9, 16, 25] + +# Lambda with filter +even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) +print(even_numbers) # [2, 4] + +# Lambda with sorting +people = [("Alice", 25), ("Bob", 30), ("Charlie", 20)] +people.sort(key=lambda person: person[1]) # Sort by age +print(people) # [('Charlie', 20), ('Alice', 25), ('Bob', 30)] + diff --git a/local_ global.py b/local_ global.py index e69de29..a7a72af 100644 --- a/local_ global.py +++ b/local_ global.py @@ -0,0 +1,23 @@ +# Global variable +global_var = 10 + +def my_function(): + # Local variable + local_var = 5 + print(f"Local: {local_var}") + print(f"Global (read): {global_var}") + +my_function() +# print(local_var) # ❌ NameError: local_var not defined + +# Modifying global variable +counter = 0 + +def increment(): + global counter # Need global keyword to modify + counter += 1 + print(f"Counter: {counter}") + +increment() # Counter: 1 +increment() # Counter: 2 +print(counter) # 2 \ No newline at end of file diff --git a/nested_if.py b/nested_if.py index e69de29..f5679a8 100644 --- a/nested_if.py +++ b/nested_if.py @@ -0,0 +1,120 @@ +# int age = 25; +# bool hasLicense = true; +# bool hasID = true; + +# if (age >= 18) { +# cout << "Age check passed" << endl; + +# if (hasLicense) { +# cout << "Has license" << endl; + +# if (hasID) { +# cout << "Has ID - Can drive!" << endl; +# } else { +# cout << "Needs ID" << endl; +# } +# } else { +# cout << "Needs license" << endl; +# } +# } else { +# cout << "Too young to drive" << endl; +# } + +# in python + + +age = 25 +has_license = True +has_id = True + +if (age >= 18): # Level 1 + print("Age check passed") # Level 1 + + if (has_license): # Level 2 + print("Has license") # Level 2 + + if (has_id): # Level 3 + print("Has ID - Can drive!") # Level 3 + else: # Level 3 + print("Needs ID") # Level 3 + else: # Level 2 + print("Needs license") # Level 2 +else: # Level 1 + print("Too young to drive") # Level 1 + + + + + +# c++ +# #include +# #include +# using namespace std; + +# int main() { +# string username, password; +# bool accountLocked = false; +# int loginAttempts = 0; + +# cout << "Enter username: "; +# cin >> username; + +# if (username == "admin") { +# cout << "Username found" << endl; + +# cout << "Enter password: "; +# cin >> password; + +# if (accountLocked) { +# cout << "Account is locked" << endl; +# } else { +# if (password == "admin123") { +# cout << "Login successful!" << endl; + +# if (loginAttempts > 3) { +# cout << "Warning: Multiple failed attempts" << endl; +# } +# } else { +# cout << "Incorrect password" << endl; +# loginAttempts++; + +# if (loginAttempts >= 5) { +# cout << "Account locked due to too many attempts" << endl; +# accountLocked = true; +# } +# } +# } +# } else { +# cout << "Username not found" << endl; +# } + +# return 0; +# } + +# in python + +username = input("Enter username: ") +password = input("Enter password: ") +account_locked = False +login_attempts = 0 + +if username == "admin": # Level 1 + print("Username found") # Level 1 + + if account_locked: # Level 2 + print("Account is locked") # Level 2 + else: # Level 2 + if password == "admin123": # Level 3 + print("Login successful!") # Level 3 + + if login_attempts > 3: # Level 4 + print("Warning: Multiple failed attempts") # Level 4 + else: # Level 3 + print("Incorrect password") # Level 3 + login_attempts += 1 # Level 3 + + if login_attempts >= 5: # Level 4 + print("Account locked due to too many attempts") # Level 4 + account_locked = True # Level 4 +else: # Level 1 + print("Username not found") # Level 1 \ No newline at end of file diff --git a/strings,methods,slicing.py b/strings,methods,slicing.py index cf62e8f..cf8c3de 100644 --- a/strings,methods,slicing.py +++ b/strings,methods,slicing.py @@ -33,7 +33,7 @@ print(b.strip()) # removes whitespace from both ends of the string print(b.lstrip()) # removes whitespace from left end of the string print(b.rstrip("!")) # removes whitespace or the mark you type -# in bracket # from right end of the string +# in bracket from right end of the string print(b.replace("!", "?")) # replaces all occurrences of "!" with "?" countstr=b.count("a") @@ -62,4 +62,5 @@ print(c.istitle()) # returns True if the string have uppercase character # at the start of each word -print(c.title()) # converts first character of each word to uppercase \ No newline at end of file +print(c.title()) # converts first character of each word to uppercase + diff --git a/switch case.py b/switch case.py index 18de552..385e189 100644 --- a/switch case.py +++ b/switch case.py @@ -13,6 +13,8 @@ case 5: print("Friday") case _: # Default case (like default: in C++) + # if day is not 1, 2, 3, 4, or 5, this block will execute + # its just like else print("Weekend") @@ -36,4 +38,3 @@ # default: # cout << "Weekend" << endl; # } - \ No newline at end of file diff --git a/while_loop.py b/while_loop.py index f35a4cb..e7c76aa 100644 --- a/while_loop.py +++ b/while_loop.py @@ -25,6 +25,8 @@ user_input = input("Enter 'quit' to exit: ") print("You entered: ,user_input") +# f string is also used to format the string in python and +# it is more efficient than + operator to concatenate the string and variable. # Infinite loop with break while True: command = input("Enter command: ")