




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Some information about python for people stuyding data analysis
Typology: Essays (university)
1 / 136
This page cannot be seen from the preview
Don't miss anything!
#1.basic excercise for begineers #2.python input and output exercises #3.files and exception #4.python loop exercises #5.python function exercises #6.python string exercises #7.python data structure exercises #8.python list exercises #9.python dictionary exercises #10 python set exercises #11.python tuple exercises #12.python date and time exercises #13.python OOP exercises #14.python JSON exercises #15.python Numpy exercises #16.python pandas exercises #17.python matplotlib exercises #18.python random data generation exercises
Excercise 1: Swap the number without using third varible
In [103]: Exercise 2: Write a Program to extract each digit from an integer in the reverse order. For example, If the given int is 7536, the output shall be “6 3 5 7“, with a space separating the digits. In [9]: Excercise 3: Write a program that will give you the sum of 3 digits In [35]: Excercise 4: Write a program that will reverse a four digit number.Also it checks whether the reverse enter the first number 4 enter the Second number 5 4 5 5 4 enter the number: 1234 4 3 2 1 enter three digit number 18 x = int(input("enter the first number ")) y = int(input("enter the Second number ")) print(x,y) x = x + y y = x - y x = x - y print(x,y) n = input("enter the number: ") y = n[:: - 1 ] k = " ".join(y) print(k) x = int(input("enter three digit number")) a = x % 10 # we will get last digit num = x // 10 # integer-divison here we will get first two digit b = num % 10 # here we will get last digit of two digit number c = num // 10 # here will get first digit of two digit number print(a + b + c)
In [41]: Excercise 6: Write a program that will tell whether the given number is divisible by 3 & 6. In [49]: Excercise 7: Write a program that will take three digits from the user and add the square of each digit. x1: 4 y1: 5 x2: 6 y2: 8 ========================================================================== ========================== Eucledian distance for given co-ordinate will be 3. enter the number 45 the number is not divisible by 3 and 6 x1 = float(input("x1: ")) y1 = float(input("y1: ")) x2 = float(input("x2: ")) y2 = float(input("y2: ")) x = [x1,y1] y = [x2,y2] print("=" ***** 100 ) print("Eucledian distance for given co-ordinate will be",round(math.dist(x,y), 2 )) num = int(input("enter the number ")) if num % 3 == 0 and num % 6 == 0 : print("the number is divisible by 3 and 6") else : print("the number is not divisible by 3 and 6")
In [53]: Excercise 8: Write a program that will check whether the number is armstrong number or not. In [54]: Excercise 9:Write a program that will take user input of (4 digits number) and check whether the number is narcissist number or not. Enter three digit number: 345 50 Enter three digit number: 121 the number is not armstrong number n = int(input("Enter three digit number: ")) # a = n % 10 # num = n // 10 # b = num % 10 # c = num // 10 # output_value = (a ****** 2 ) + (b ****** 2 ) + (c ****** 2 ) print(output_value) An Armstrong number is one whose sum of digits raised to the power three equals the number itself. 371, for example, is an Armstrong number because 33 + 73 + 1**3 =
n = int(input("Enter three digit number: ")) a = n % 10 # num = n // 10 # b = num % 10 # c = num // 10 # if (a ****** 3 + b ****** 3 + c ****** 3 ) == n: print("the number is armstrong number") else : print("the number is not armstrong number")
In [137]: Excercise 5:Print all factors of a given number provided by the user. In [81]: Exercise 6: Accept a list of 5 float numbers as an input from the user In [144]:
Exercise 7: Accept any three string from one input() call
enter the number: 45 1 3 5 9 15 45 enter the number: 67. enter the number: 98. enter the number: 655. enter the number: 78. enter the number: 65. [67.65, 98.98, 655.65, 78.9997, 65.545] num = 56. y = float(round(num, 2 )) print(y) n = int(input("enter the number: ")) for i in range( 1 ,n + 1 ): if n % i == 0 : print(i,end = " ") l1 = [] while len(l1) < 5 : n = float(input("enter the number: ")) l1.append(n) print(l1)
In [196]: Exercise 8: Format variables using a string.format() method. Write a program to use string.format() method to format the following three variables as per the expected output In [204]: Excercise 9: Write a program to find the simple interest when the value of principle,rate of interest and time period is given. enter the three names with keeping space in between: govind damodar madhav eti govind damodar madhaveti i have 1200 dollars so i can buy 4 football for 450 dollars inp = input("enter the three names with keeping space in between: ") sep = inp.split() #print(sep) name1 = sep[ 0 ] name2 = sep[ 1 ] name3 = sep[ 2 ] print(name1) print(name2) print(name3) totalmoney = 1200 quantity = 4 price = 450 print("i have {} dollars so i can buy {} football for {} dollars".format(totalmoney,quant
Exceptions are special objects that help your programs respond to errors in appropriate ways. For example if Exercise 1: Check file is empty or not,Write a program to check if the given file is empty or not In [205]: Excercise 2:read content from the file In [2]: Excercise 3:# read line by line In [3]: file is not empty i'm aspiring data scientist i love to work with data i love making the games i love python i'm aspiring data scientist i love to work with data i love making the games i love python import os size = os.stat("test file.txt").st_size if size == 0 : print("file is empty") else : print("file is not empty") filename = "siddharth.txt" with open(filename) as f_obj: content = f_obj.read() print(content) filename = "siddharth.txt" with open (filename) as f_obj: for line in f_obj: print(line.rstrip())
In [4]: Excercise 4:now we will learn about writing to the files passing 'w' argument to open() tells python you want to write to the file be carefull: this will erase the contents of the file if it already exists passing the 'a' arguement tell python you want to append to the end of an existing file In [5]: Excercise 5: writing multiple line to empty file In [7]: In [8]: Exercise 6: Write all content of a given file into a new file by skipping line number 5 i'm aspiring data scientist i love to work with data i love making the games i love python #storing line to the list filename = "siddharth.txt" with open(filename) as f_obj: lines = f_obj.readlines() for line in lines: print(line.rstrip()) filename = "programming.txt" with open(filename,'w') as f: f.write("i love machine learning as well") with open(filename,'w') as f: f.write("Deep learning is awesome.\n") f.write("AI is future\n") with open(filename,'a') as f: f.write("i love to work with data science projects \n") f.write("i love making the application")
filename="< file path >" with open(filename) as f: lines=f.readlines()
Excercise 8: Let see handling zero division exceptional error In [10]: In [11]: In [11]: the else block the try block should only contain the code that may cause an error any code that depends on try block running succesfully should be placed in the else block Excercise 9:using else block
ZeroDivisionError Traceback (most recent call las t) ~\AppData\Local\Temp\ipykernel_13444\1152173066.py in
In [13]: preventing crashes from user input without except block in following example , the programe would crash if user try to divide by zero as written it will handle the error gracefully and keep running In [14]: Excercise 10: how does finally block work in python enter the number: 5 enter the number: 0 hey you...denominater cant be zero enter the numbers: enter the 'q' to exit enter the number: enter the number:
enter the number:q x = int(input("enter the number: ")) y = int(input("enter the number: ")) try : result = x / y except ZeroDivisionError: print("hey you...denominater cant be zero") else : print(result) # create simple calculater print("enter the numbers: ") print("enter the 'q' to exit") while ( True ): x = input("enter the number:") if x == 'q': break y = input("enter the number:") if y == 'q': break try : result = int(x) / int(y) except ZeroDivisionError: print("bhidu zero se divide nahi kar sakate") else : print(result)
Exercise 3:Write a program to check if the given number is a palindrome number. A palindrome number is a number that is same after reverse. For example 545, is the palindrome numbers In [14]: Exercise 4: Create a new list from a two list using the following condition. Given a two list of numbers, write a program to create a new list such that the new list should contain odd numbers from the first list and even numbers from the second list. In [100]: Exercise 5: Calculate income tax for the given income by adhering to the below rules first 10k--> 0% second 10 --> 10% remaining-->20% Expected Output: enter the number: 343 343 the given number is palindrom [1, 3, 5, 6, 8, 10] n = input("enter the number: ") rev_number = n[:: - 1 ] print(rev_number) if n == rev_number: print("the given number is palindrom") else : print("the given number is not palindrom") l1 = [ 1 , 2 , 3 , 4 , 5 ] l2 = [ 6 , 7 , 8 , 9 , 10 ] l3 = [] for i in l1: if i % 2 != 0 : l3.append(i) for i in l2: if i % 2 == 0 : l3.append(i) print(l3)
For example, suppose the taxable income is 45000 the income tax payable is 10000 0% + 10000 10% + 2500020% = $6000.* In [96]: In [15]: Exercise 6: Print multiplication table form 1 to 10 enter the number: 45000
enter the number: 45000
n = int(input("enter the number: ")) if n <= 10000 : print(n) else : a = n - 10000 b = a - 10000 c = b ***** 0. d = c + 1000 print(d) # Another way of doing the same n = int(input("enter the number: ")) a = 10000 b = 10000 c = n - (a + b) tax = 0 if n <= a: tax = 0 elif n > a and n < 20000 : tax = b ***** 0. else : tax = (b ***** 0.1) + (c ***** 0.2) print(tax)
In [69]: **Excercise 8: The current population of a town is 10000. The population of the town is increasing at the rate of 10% per year. You have to write a program to find out the population at the end of each of the last 10 years. For eg current population is 10000 so the output should be like this:
armstrong_list = [] for i in range( 100 , 1000 ): a = i % 10 # 123--> num = i // 10 #123--> b = num % 10 #---> c = num // 10 #--> if (a ****** 3 ) + (b ****** 3 ) + (c ****** 3 ) == i: armstrong_list.append(i) print(armstrong_list) p = 10000 for i in range( 1 , 10 ): p = p - 0.1 ***** p print(round(p),end = " ")
In [72]: Excercise 10:Write a program to print whether a given number is prime number or not In [68]: Excercise 11: User will provide 2 numbers you have to find the HCF of those 2 numbers
enter the number: 45 it is not a prime number # since we have 4 digit there unique combination will be 16 for i in range( 1 , 5 ): for j in range( 1 , 5 ): if i != j: print(i,j) x = int(input("enter the number: ")) new = [] if x == 1 : print("1 is not prime number") else : for i in range( 1 ,x + 1 ): if x % i == 0 : new.append(i) #print(new) if len(new) > 2 : print("it is not a prime number") else : print("it is prime number ")