Wednesday, January 18, 2012

Python Note 1

Python is an interpreted language. Nesting is done using indentation. I have used Python 3.2.2 in this note.


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&gt;75 and marks &lt;=100 :
 grade="A"
elif marks &gt;= 65 and marks &lt;= 75 :
 grade="B"
elif marks &gt;= 55 and marks &lt; 65 :
 grade="C"
elif marks &gt;= 45 and marks &lt; 55 :
 grade="S"
elif marks &lt; 45 and marks &gt;= 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
• Write a program to Print numbers 1 to 9

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() command generates a list. list is a data structure very similar to an array. By the way list can contain data of different types.
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

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis