From 1c60b20ca85fba4b7c1fe134c9e37e1e4dad136e Mon Sep 17 00:00:00 2001 From: rksh001 <38465449+rksh001@users.noreply.github.com> Date: Fri, 17 Jan 2025 19:23:33 +0100 Subject: [PATCH] update --- Python Practice/01_main.py | 4 ++ Python Practice/02_Variables.py | 39 ++++++++++++++++ Python Practice/03_Typecasting.py | 28 ++++++++++++ Python Practice/04_Input.py | 10 +++++ Python Practice/05_Excercise-01.py | 48 ++++++++++++++++++++ Python Practice/06_Math.py | 62 ++++++++++++++++++++++++++ Python Practice/06_Math02.py | 42 +++++++++++++++++ Python Practice/07_IfStatement.py | 44 ++++++++++++++++++ Python Practice/08_Exercise-02.py | 53 ++++++++++++++++++++++ Python Practice/09_LogicalOperators.py | 28 ++++++++++++ Python Practice/10_ConditionalExp.py | 31 +++++++++++++ Python Practice/11_StringMethods.py | 40 +++++++++++++++++ Python Practice/12_Exercise_03.py | 22 +++++++++ Python Practice/13_Indexing.py | 22 +++++++++ 14 files changed, 473 insertions(+) create mode 100644 Python Practice/01_main.py create mode 100644 Python Practice/02_Variables.py create mode 100644 Python Practice/03_Typecasting.py create mode 100644 Python Practice/04_Input.py create mode 100644 Python Practice/05_Excercise-01.py create mode 100644 Python Practice/06_Math.py create mode 100644 Python Practice/06_Math02.py create mode 100644 Python Practice/07_IfStatement.py create mode 100644 Python Practice/08_Exercise-02.py create mode 100644 Python Practice/09_LogicalOperators.py create mode 100644 Python Practice/10_ConditionalExp.py create mode 100644 Python Practice/11_StringMethods.py create mode 100644 Python Practice/12_Exercise_03.py create mode 100644 Python Practice/13_Indexing.py diff --git a/Python Practice/01_main.py b/Python Practice/01_main.py new file mode 100644 index 0000000000..09797c609a --- /dev/null +++ b/Python Practice/01_main.py @@ -0,0 +1,4 @@ +# This is my first Python program +print ("I like pizza") +print ("It's really good!") + diff --git a/Python Practice/02_Variables.py b/Python Practice/02_Variables.py new file mode 100644 index 0000000000..e4ec8219e2 --- /dev/null +++ b/Python Practice/02_Variables.py @@ -0,0 +1,39 @@ +#Strings +first_name = "Rakesh" +print(first_name) +print(f"Hello {first_name}") +last_name = "Tiwarekar" +print(f"My name is {first_name} {last_name}") + +#Integers +age = 40 +print(age) +print(f"You are {age} years old") +quantity = 3 +print(f"You are buying {quantity} items") +num_of_students = 20 +print(f"Your class has {num_of_students} students") + +#Float +price = 19.99 +print(f"The price is ${price}") +gpa = 3.5 +print(f"Your GPA is {gpa}") +distance = 5.5 +print(f"The distance is {distance} kilometers") + +#Boolean + +is_student = True +print(f"Are you a student? {is_student}") +is_teacher = False +if is_student: + print("You are a student") +else: + print("You are not a student") + +is_online = True +if is_online: + print("You are online") +else: + print("You are offline") diff --git a/Python Practice/03_Typecasting.py b/Python Practice/03_Typecasting.py new file mode 100644 index 0000000000..2b190fe1f8 --- /dev/null +++ b/Python Practice/03_Typecasting.py @@ -0,0 +1,28 @@ +#Typecasting = process of converting a variable from one data type to another data type +name= "Rakesh Tiwarekar" +age = 40 +gpa = 3.5 +is_student = True +type(name) +print(type(name)) +print(type(age)) +print(type(gpa)) +print(type(is_student)) + +gpa=int(gpa) +print(type(gpa)) +age=float(age) +print(type(age)) + +age=str(age) #age is now a string +print(type(age)) +print(age) #age is now a string + +age += "1" +print(age) #age is now a string + +name = bool(name) +print(type(name)) #name is now a boolean +print(name) #name is now a boolean + + diff --git a/Python Practice/04_Input.py b/Python Practice/04_Input.py new file mode 100644 index 0000000000..7b6fda4e5c --- /dev/null +++ b/Python Practice/04_Input.py @@ -0,0 +1,10 @@ +#Input = A function that prompts the user for input and returns the input as a string + +name = input("What is your name? :") +print(f"Hello, {name}!") + + +age = int(input("How old are you? :") ) +age = age + 1 +print("HAPPY BIRTHDAY!!!") +print(f"You are {age} years old") diff --git a/Python Practice/05_Excercise-01.py b/Python Practice/05_Excercise-01.py new file mode 100644 index 0000000000..201eb4ccc8 --- /dev/null +++ b/Python Practice/05_Excercise-01.py @@ -0,0 +1,48 @@ +#Exercise 1 - Rectangle Area calculation + +length = float(input("Enter the length of the rectangle: ")) +width = float(input("Enter the width of the rectangle: ")) + +Area = length * width +print("The area of the rectangle is: ", Area) + +print(f"The area of the rectangle is: {Area} cm2") + + +#Execise 2 - Shopping cart program + +item = input("What item would you like to buy? ") +price=float(input("What is the price of the item? ")) + +quantity = int(input("How many would you like to buy? ")) +total = price * quantity + +print(f"The total price of {quantity} {item} is: {total} ") + +print(f"You have purchased {quantity} {item} at {price} each for a total of ${total} ") + + +#Madlibs Game +adjective1 = input("Enter an adjective(description): ") +noun1 = input("Enter a noun(person, place, thing): ") +adjective2= input("Enter an adjective(description): ") +verb1 = input("Enter a verb ending with 'ing': ") +adjective3 = input("Enter an adjective(description): ") + + +print(f"Today I went to a {adjective1} zoo.") +print(f"In an exibit, I saw a {noun1}.") +print(f"{noun1} was {adjective2} and {verb1}.") +print(f"I was {adjective3}.") + + + + + + + + + + + + diff --git a/Python Practice/06_Math.py b/Python Practice/06_Math.py new file mode 100644 index 0000000000..ed07192c6a --- /dev/null +++ b/Python Practice/06_Math.py @@ -0,0 +1,62 @@ + +friends = 5 + +#ADDITION +friends = friends + 1 +print(friends) +#auguemented assignment operator +friends += 1 +print(friends) + +#SUBTRACTION +friends = friends -2 +print(friends) +#auguemented assignment operator +friends -= 2 +print(friends) + +#MULTIPLICATION +friends = friends * 3 +print(friends) +#auguemented assignment operator +friends *= 3 +print(friends) + +#DEVISION +friends = friends / 2 +print(friends) +#auguemented assignment operator +friends /= 2 +print(friends) + +#EXPONENT +friends = friends ** 2 +print(friends) + +#auguemented assignment operator +friends **=2 +print(friends) + +#REMAINDER - Modulus operator +remainder = friends % 3 +print(remainder) + +#---------------------------------------------- +x=3.14 +y=-4 +z=5 + +result = round(x) #rounds to the nearest whole number +print(result) + +result = abs(y) #returns the absolute value of y +print(result) + +result = pow(y,3) #returns y raised to the power of 3 +print(result) + +result = (x,y,z) #returns the largest number +print(result) + +result = min(x,y,z) #returns the smallest number +print(result) \ No newline at end of file diff --git a/Python Practice/06_Math02.py b/Python Practice/06_Math02.py new file mode 100644 index 0000000000..da279e6756 --- /dev/null +++ b/Python Practice/06_Math02.py @@ -0,0 +1,42 @@ +import math #importing math module + +print(math.pi) +print(math.e) + +x=9 +result = math.sqrt(x) #returns the square root of x +print(result) + +y=9.1 +result =math.ceil(y) #returns the smallest integer greater than or equal to y +print(result) + +z=9.9 +result = math.floor(z) #returns the largest integer less than or equal to z +print(result) + + +#Calculate circumference of a circle + +radius = float(input("Enter the radius of the circle: ")) +circumference = 2 * math.pi * radius +print(f"The circumference of the circle is {round(circumference,2)}") + +#Calculate area of a circle + +radius = float(input("Enter the radius of the circle: ")) +area = math.pi * pow(radius,2) +print(f"The area of the circle is {round(area,2)}") + +#Hytothenuse of a right angled triangle + +a = float(input("Enter the length of side a: ")) +b = float(input("Enter the length of side b: ")) +c = math.sqrt(pow(a,2) + pow(b,2)) +print(f"The length of the hypotenuse is {round(c,2)}") + + + + + + diff --git a/Python Practice/07_IfStatement.py b/Python Practice/07_IfStatement.py new file mode 100644 index 0000000000..48fef6000a --- /dev/null +++ b/Python Practice/07_IfStatement.py @@ -0,0 +1,44 @@ +#if=Do some code if a condition is true +#Else=Do some code if a condition is false + +age = int(input("Enter your age: ")) +if age > 100: + print("You are too old to sign up.") +elif age <0: + print("You are not born yet.") +elif age >= 18: + print("You are now signed up!") +else: + print("You are not old enough to sign up.") + + +#Use == for comparison +response = input("Would you like food (yes/no): ") +if response == "yes": + print("Here is your food.") +else: + print("Okay, no food for you.") + + +name = input("What is your name: ") + +if name == "": + print("You did not enter a name.") +else: + print(f"Hello, {name}!") + +#Boolean +for_sale = True +if for_sale: + print("Item is for sale.") +else: + print("Item is not for sale.") + +#Boolean +online = False + +if online: + print("User is online.") +else: + print("User is offline.") + diff --git a/Python Practice/08_Exercise-02.py b/Python Practice/08_Exercise-02.py new file mode 100644 index 0000000000..9e9d5302c4 --- /dev/null +++ b/Python Practice/08_Exercise-02.py @@ -0,0 +1,53 @@ +#python calculator + + +operator = input("Enter the operator (+ - * /): ") +num1 = float(input("Enter the first number: ")) +num2 = float(input("Enter the second number: ")) + +if operator == "+": + result = num1 + num2 + print(round(result,3)) +elif operator == "-": + result = num1 - num2 + print(round(result,3)) +elif operator == "*": + result = num1 * num2 + print(round(result,3)) +elif operator == "/": + result = num1 / num2 + print(round(result,3)) +else: + print(f"Invalid operator: {operator}") + + +# Python weight converter + +weight = float(input("Enter your Weight: ")) +unit = input("Kilograms or Pounds? (K or L): ") + +if unit == "K": + weight = weight * 2.205 + unit = "Lbs" + print(f"Your weight is {weight} and unit is {unit}") +elif unit == "L": + weight = weight / 2.205 + unit = "Kg" + print(f"Your weight is {weight} and unit is {unit}") +else: + print('Invalid unit') + + +#Temperature Converter + +unit = input("Is this temperature in Celsius or Fahrenheit? (C/F): ") +temp = float(input("Enter the temperature: ")) + +if unit == "C": + temp = round((9*temp) /5 + 32,1) + print(f"The temperature is {temp} degrees Fahrenheit.") +elif unit == "F": + temp = round((5*(temp-32))/9,1) + print(f"The temperature is {temp} degrees Celsius.") +else: + print(f"Invalid unit: {unit}") diff --git a/Python Practice/09_LogicalOperators.py b/Python Practice/09_LogicalOperators.py new file mode 100644 index 0000000000..d9a15b4afd --- /dev/null +++ b/Python Practice/09_LogicalOperators.py @@ -0,0 +1,28 @@ +#Logical operators = evaluate multiple conditions (or, and, not) +# or = either condition is true +# and = both conditions are true +# not = reverse the result, returns False if the result is true + + +temp = 20 +is_raining = True + +if temp > 35 or temp < 0 or is_raining: + print("The outdoor event is cancelled") +else: + print("The outdoor event is not cancelled") + + +temp = 20 +is_sunny = False + +if temp >= 28 and is_sunny: + print("It is HOT outside") +elif temp <= 0 and is_sunny: + print("It is COLD outside") +elif 28 > temp > 0 and is_sunny: + print("It is WARM outside") +elif 28 > temp > 0 and not is_sunny: + print("It is CLOUDY outside") +else: + print("It is neither hot nor cold outside") \ No newline at end of file diff --git a/Python Practice/10_ConditionalExp.py b/Python Practice/10_ConditionalExp.py new file mode 100644 index 0000000000..ab5b5e56a2 --- /dev/null +++ b/Python Practice/10_ConditionalExp.py @@ -0,0 +1,31 @@ +#conditional expressions = A one-line shortcut for the if-else statement (ternary operator) +# Print or assign one of the two values based on a condition +# X if condition else Y + + +num = 5 +print("Positive" if num > 0 else "Negative") # Positive + +results = "EVEN" if num % 2 == 0 else "ODD" +print(results) # ODD + +a=6 +b=5 +age = 25 +temperature = 30 +user_role = "admin" + + +#Age +max_num = a if a>b else b +min_num = a if a < b else b +status = "Adult" if age >= 18 else "Child" +print (status) # Adult + +#Weather +weather = "HOT" if temperature > 20 else "COLD" +print(weather) # HOT + +#Access Level +access_level = "Full Access" if user_role == "admin" else "Limited Access" +print(access_level) # Full Access diff --git a/Python Practice/11_StringMethods.py b/Python Practice/11_StringMethods.py new file mode 100644 index 0000000000..6ad76cde2e --- /dev/null +++ b/Python Practice/11_StringMethods.py @@ -0,0 +1,40 @@ + +# String functions + +name = input("Enter your full name: ") +phone_number = input("Enter your phone number: ") + +result = len(name) +print(result) + + +result = name.find("T") +print(result) + +#Reverse find +result = name.rfind("T") +print(result) + +name = name.capitalize() # First letter capital +print(name) + +name = name.upper() # All letters capital +print(name) + +name = name.lower() # All letters small +print(name) + +result = name.isdigit() # Check if all letters are digits +print(result) + +result = name.isalpha() # Check if all letters are alphabets +print(result) + +result = phone_number.count("-") # Count the number of times "-" appears in the phone number +print(result) + +phone_number = phone_number.replace("-", " ") # Replace "-" with " " +print(phone_number) + + +print(help(str)) # Get help on string functions \ No newline at end of file diff --git a/Python Practice/12_Exercise_03.py b/Python Practice/12_Exercise_03.py new file mode 100644 index 0000000000..0c798e48e7 --- /dev/null +++ b/Python Practice/12_Exercise_03.py @@ -0,0 +1,22 @@ +# Validate user input exercise +# 1. username is no more than 12 characters +# 2. username must not contain spaces +# 3. username must not contain digits + +username = input("Enter your username: ") +username.find(" ") + + + +if len(username) > 12: + print("Username can't be long than 12 characters") +elif not username.find(" ") == -1: + print("Username can't contain spaces") +elif username.isdigit(): + print("Username can't contain digits") +elif not username.isalpha(): + print("Username can't contain numbers") +else: + print(f"Welcome {username}") + + diff --git a/Python Practice/13_Indexing.py b/Python Practice/13_Indexing.py new file mode 100644 index 0000000000..85b6f107ea --- /dev/null +++ b/Python Practice/13_Indexing.py @@ -0,0 +1,22 @@ +#indexing = accessing elements of a sequence using [] (Indexing operator) +# [start : end : step] - slicing operator + +credit_number = "1234-5678-9013-4567" +credit_number[0] # 1 +print(credit_number[2]) # 1 +print(credit_number[0:4] ) # 1234 +print(credit_number[5:9]) # 5678 +print(credit_number[5:]) # 5678-9013-4567 +print(credit_number[-2]) # 6 + +print(credit_number[::4]) # 1-7-0-6 + + +last_digits = credit_number[-4:] +print(f"XXXX-XXXX-XXXX-{last_digits}") # XXXX-XXXX-XXXX-4567 + +credit_number = credit_number[::-1] +print(credit_number) # 7654-3109-8765-4321 + + +