Sample python program demonstrating Python operators.
x=10 y=12 print(x+y) print(x-y) print(y/x) print(y//x) print(x*y) print(y%x) print(x**y) print(-x)Result
Sample python program demonstrating input capabilities and string concatenation in python.
numInt=int(input("Enter an integer : ")) numFloat=float(input("Enter a float :")) strName=input("Enter a String : ") numTot=numInt+numFloat print("Hello " + strName + " Total is " + str(numTot))Result
• Write a python program to input a length in mm and convert it to inches.
• 25.4 mm = 1 inch
lenmm=float(input("Enter length in mm : ")) leninch=(1/25.4)*lenmm print("Length in inches : " + str(leninch));• Write a python program to input a temperature in Celcius and convert it to Fahrenheit.
• fahrenheit = 9/5* celcius +32
tempinCelc=float(input("Enter temperature in celcious : ")) tempinFer=(9/5)*tempinCelc + 32 print("Temp in Fahrenheit : " + str(tempinFer))• Write a program to input a mark and display Pass if it is greater than or equal to 50.
Otherwise display Fail.
marks=int(input("Enter your marks : ")) if marks >= 50 : print("Pass!") else: print("Fail!")• Write a program to input a number. Display if it is an even or an odd number.
number=int(input("Enter a number : ")) if number%2 == 0 : print("Even number") else: print("Odd number")
• Write a program to input the marks of a
student and calculate the grades.
• > 75 – A
• 65 to 75 – B
• 55 to 64 – C
• 45 to 54 – S
• < 45 - W
#this is not an efficient way to calculate grade #this is only for demonstrating if,elif,else statements marks=int(input("Enter your marks : ")); if marks>75 and marks <=100 : grade="A" elif marks >= 65 and marks <= 75 : grade="B" elif marks >= 55 and marks < 65 : grade="C" elif marks >= 45 and marks < 55 : grade="S" elif marks < 45 and marks >= 0 : grade="W" else : grade="Invalid Marks" print("Your Grade is : " + grade)Note : Above code is intended to demonstrate conditions, if, elsif statements.
• Write a python program to implement following algorithm
number=int(input("Enter a number : " )); if number > 0 : if number%3==0 : print("The number is positive and divisable by 3") else : print("The number is zero or negative")
- While Condition
num=1 while num < 10 : print(num) num+=1 print("Loop ended!");
• Write a program to Print numbers
1,3,5,7,9
i=0 while i<10 : if i%2==1: print(i) i+=1• Write a program to print the tables of 7
num=1 while num <14 : print ("7 x " + str(num) +" = " + str(7*num) ) num+=1
- range() command
• range(1,10) produces the list
[1,2,3,4,5,6,7,8,9]
• for loop can be used to iterate within a list
numlist=range(1,10) for val in numlist : print(val)
numlist=range(1,10,2) for val in numlist : print(val)
- Defining a list explicitly
mylist=["Java","PHP",1133,"Python",2.33] for val in mylist : print(val)it works same for strings.
mylist="Sri Lanka" for val in mylist : print(val)
0 comments:
Post a Comment