In this presentation you will learn how to start basic programming of Python.
You get a new presentation every Sunday at 10 AM.
Size: 507.02 KB
Language: en
Added: Jun 06, 2021
Slides: 19 pages
Slide Content
Basic Python Programs
After writing this program you save this program hello .py (every program save in python with . py extension). Hello world program # This program print Hello, world! print('Hello, world!')
# Store input numbers n1 = input('Enter first number: ') n2 = input('Enter second number: ') sum = float(num1) + float(num2) Print(“the sum of two num is” ,sum) Output Enter first number: 1.1 Enter second number: 1.3 The sum of two num is 2.4 ADD TO NUMBERS
a = float(input('Enter first num: ')) b = float(input('Enter second num: ')) c = float(input('Enter third num: ')) s = (a + b + c) / 2 Print(“Area of Triangle is”, s) Find Area of triangle
Enter first num: 8 Enter second num: 4 Enter third num: 2 The area of the triangle is 7 Output
In computer programming, swapping two variables specifies the mutual exchange of values of the variables. It is generally done by using a temporary variable. Swapping
Variable Swapping x = input('Enter first value: ') y = input('Enter second value: ') temp = x x = y y = temp print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y))
Calculate Simple Interest P = float(input('Enter Principal: ')) R = float(input('Enter Rate: ')) T = float(input('Enter Time: ')) SI = (P * R *T) /100 print(“Simple Interest is”, SI) Enter Principal:2000 Enter Rate: 4 Enter Time: 2 Simple Interest is 160 Output
# importing calendar module import calendar yy = 2021 # year mm = 9 # month # display the calendar print( calendar.month ( yy , mm)) September 2021 Generate Calendar Note- we import the calendar module. The built-in function month() inside the module takes in the year and the month and displays the calendar for that month of the year.
Solve Quadratic Equation # import complex math module import cmath a = float(input('Enter a: ')) b = float(input('Enter b: ')) c = float(input('Enter c: ')) # calculate the discriminant d = (b**2) - (4*a*c) # find two solutions sol1 = (-b- cmath.sqrt (d))/(2*a) sol2 = (- b+cmath.sqrt (d))/(2*a) print('The solution are {0} and {1}'.format(sol1,sol2))
Output Enter a: 8 Enter b: 5 Enter c: 9 The solution are (-0.3125-1.0135796712641785j) and (-0.3125+1.0135796712641785j)
n = int (input("Enter a decimal number: ")) print(bin( dec ),"in binary.") print( oct ( dec ),"in octal.") print(hex( dec ),"in hexadecimal.") Convert Decimal to binary, octal and hexadecimal. Output Enter a decimal number: 56 0b111000 in binary. 0o70 in octal. 0x38 in hexadecimal.
n = 12 print(type(n)) print(n) # convert the num into string con_num = str (n) # check and print type print(type( con_num )) print( con_num ) <class ' int '> 12 <class ' str '> 12 Convert int to string Output
generate a random string import string import random # define the random module S = 10 # number of characters in the string. ran = ''.join( random.choices ( string.ascii_uppercase + string.digits , k = S)) print("The randomly generated string is : " + str (ran)) # print the random data The randomly generated string is : BK8I922AM2 The randomly generated string is : VDYUPYRK32 Output
Convert list to dictionary vowels = ['a', 'e', ' i ', 'o', ' i ', 'u‘] # count element ‘o' count = vowels.count ('o’) # print count print('The count of o is:', count) # count element 'r' count = vowels.count ('r’) print('The count of r is:, count) The count of i is: 1 The count of r is: 0 Output
Membership test in tuple tuple = ('p', 'y', 't', 'h', ' o','n ') # In operation print('y' in tuple ) print('w' in tuple ) # Not in operation print('s' not in my_tuple ) True False True Output