Basic python programs

RaginiJain21 1,863 views 19 slides Jun 06, 2021
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

In this presentation you will learn how to start basic programming of Python.
You get a new presentation every Sunday at 10 AM.


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

dic = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50} print(dic.pop(3)) print( dic ) # remove the last item, return ( key,value ) print( dic.popitem ()) print( dic ) # remove all items dic.clear () Print( dic ) 30 {1: 10, 2: 20, 4: 40, 5: 50} (5, 50) {1: 10, 2: 20, 4: 40} {} Output Removing elements from a dictionary

For more presentation contact us on [email protected]