Skip to content
Merged
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
9 changes: 8 additions & 1 deletion day 1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +9 to +12

# 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
Expand Down
18 changes: 17 additions & 1 deletion f_string.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,4 +38,18 @@

# 8. Debugging (Python 3.8+)
x = 10
print(f"{x = }") # Prints: x = 10
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
1 change: 1 addition & 0 deletions for loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions func_argument.py
Original file line number Diff line number Diff line change
@@ -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!
Comment on lines +33 to +45

7 changes: 6 additions & 1 deletion function_overloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,3 +57,5 @@ def _(value):
process("hello") # String: hello
process([1, 2, 3]) # List with 3 items
process(3.14) # Default: 3.14


8 changes: 8 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello world")
5 changes: 3 additions & 2 deletions if else.py
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -45,4 +45,5 @@

# if x > 5:
# print("Hello") # ❌ IndentationError
# print("World") # ❌ Mixed indentation
# print("World") # ❌ Mixed indentation

15 changes: 14 additions & 1 deletion input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +9 to +17

# 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)
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
Comment on lines +27 to +28
24 changes: 24 additions & 0 deletions lambda_function.py
Original file line number Diff line number Diff line change
@@ -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)]

23 changes: 23 additions & 0 deletions local_ global.py
Original file line number Diff line number Diff line change
@@ -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
120 changes: 120 additions & 0 deletions nested_if.py
Original file line number Diff line number Diff line change
@@ -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 <iostream>
# #include <string>
# 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
Comment on lines +96 to +120
5 changes: 3 additions & 2 deletions strings,methods,slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
print(c.title()) # converts first character of each word to uppercase

3 changes: 2 additions & 1 deletion switch case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +16 to +17
print("Weekend")


Expand All @@ -36,4 +38,3 @@
# default:
# cout << "Weekend" << endl;
# }

2 changes: 2 additions & 0 deletions while_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +28 to +29
# Infinite loop with break
while True:
command = input("Enter command: ")
Expand Down