0% found this document useful (0 votes)
5 views31 pages

Programming Basics: Python Examples

The document contains various Python code snippets for different programming tasks, including temperature conversion, student mark details, area calculations, Fibonacci series, factorial calculation, counting odd/even numbers, and more. Each section includes code, expected output, and explanations for functionalities such as file handling, turtle graphics, and a hangman game. It serves as a comprehensive guide to basic programming concepts and implementations in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views31 pages

Programming Basics: Python Examples

The document contains various Python code snippets for different programming tasks, including temperature conversion, student mark details, area calculations, Fibonacci series, factorial calculation, counting odd/even numbers, and more. Each section includes code, expected output, and explanations for functionalities such as file handling, turtle graphics, and a hangman game. It serves as a comprehensive guide to basic programming concepts and implementations in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Temperature conversion

CODE:
Celsius=int(input(“Enter degree in celsius:”))
Fahrenheit=(Celsius*1.8)+32
Print (“Celsius to Fahrenheit=”,Fahrenheit)
Fahrenheit=int(input(“Enter degree in fahrenheit:”))
Celsius=(Fahrenheit-32)/1.8
Print(“Fahrenheit to Celsius=”,Celsius)
OUTPUT:
Enter degree in celsius:79
Celsius to Fahrenheit=174.20
Enter degree in fahrenheit:87
Fahrenheit to Celsius=30.55
[Link] Mark Details
CODE:
Mark1=int(input(“enter mark 1:”))
Mark2=int(input(“enter mark2:”))
Mark3=int(input(“enter mark3:”))
Mark4=int(input(“enter mark4:”))
Mark5=int(input(“enter mark5:”))
total=mark1+mark2+mark3+mark4+mark5
Print(“total=”,total)
avg=total/500
print(“average=”,avg)
ifavg>=80:
print(“Grade:A”)
ifavg>=70:
print(“Grade:B”)
ifavg>=60:
print(“Grade:C”)
ifavg>=50:
print(“Grade:D”)
ifavg>=40:
print(“Grade:E”)
else: print(“fail”)
OUTPUT:
Enter a mark1:90
Enter a mark2:99
Enter a mark3:89
Enter a mark4:98
Enter a mark5:100
Total=476
Average:95%
Grade:A
[Link] of rectangle, square, circle and triangle
CODE:
print(“program to calculate area”)
print(“[Link]”)
print([Link]”)
print(“[Link]”)
print(“[Link]”)
choice=int(input(“Enter your choice:”)
if (choice==1):
width=int(input(“Enter width:”))
length=int(input(“Enter length:”))
print(“Area of rectangle=”,(width*length))
elif (choice==2):
side=int(input(“Enter sides”))
print(“Area of square=”,(side*side))
elif(choice==3):
radius=int(input(“Enter radius:”))
print(“area of circle=”,(3.14*radius*radius))
elif (choice==4):
base=int(input(“Enter base:”))
height=int(input(“Enter height:”))
print(“Area of triangle=”,(0.5*base*height))
else:
print(“Invalid input
OUTPUT:
Program to calculate area
[Link]
[Link]
[Link]
[Link]
Enter your choice:4
enter base=33
enter height=42
Area of triangle:69
4. Fibonacci series
CODE:
n=int(input(“enter the value of ‘n’:”)
a=0
b=1
sum=0
count=1
Print(“Fibonacci series:”,end=””)
While(count<=n):
Print(sum,end=””)
While(count<=n):
Print(sum,end=””)
Count+=1
a=b
b=sum
sum=a+b
OUTPUT:
enter the value of’n’:5
fibonacci series:0 1 1 2 3 5 8 13 21 3
5. Factorial using recursion function
CODE:
def fact(n):
if (n==0 or n==1):
return 1
else:
return n*fact(n-1)
n=int(input(“Enter a number:”))
print(“the factorial of”,n,”is”,fact(n))
OUTPUT:
Enter a number:5
The factorial of 5 is 120
6. Counting Odd and Even numbers
CODE:
numbers=(90,89,78,76,55)
odd=0
even=0
for x in numbers:
if not x%2:
even+=1
else:
odd+=1
print(“total number of even :”,even)
print(“total number of odd:”,odd)
OUTPUT:
total number of even :3
total number of odd :2
7. Counting Upper and Lowercase Letters
CODE:
String=input(“enter string:”)
count1=0
count2=0
For i in string:
If([Link]()):
count1=count1+1
elif([Link]()):
count2=count2+1
print(“the number of lowercase character is :”,count1)
print(“the number of uppercase character is:”,count2)
OUTPUT:
Enter string: WelcoMEToiSm
the number of lowercase character is:7
the number of uppercase character is:5
8. Palindrome or not
CODE:
def reverse(str):
str=” “
for i in str:
rev_str= i + str
return rev_str
str=input(“Enter a string:”)
print(“The entered string:”str)
print(“the reversed string:”,reverse(str))
if(str==str[::-1]):
print(”the given string is palindrome”)
else:
print(“the given string is not palindrome”)
OUTPUT:
Enter a string: Malayalam
The entered string:Malayalam
The reversed string:malayalaM
The given string is palindrome
9. Sum of Items in Dictionary
CODE:
values={‘A’:100,’B’:410,’C’:356}
print(“the total sum of values in the dictionary:”)
print(sum([Link]()))

Output:
The total sum of value in the dictionary:866
10. Construct the Following Pattern
CODE:
rows=int(input(“Enter a number of rows:”))
For i in range (rows):
For j in range(i+1):
Print(i,end=””)
Print(“\n”)
OUTPUT:
Enter a number of rows:5
0
11
222
3333
44444
11. Read and Copy File
CODE:
fn=open('[Link]','r')
fn=open('[Link]','w')
cont=[Link]()
type(cont)
for i in range(0,len(cont)):
if(i%2!=0):
[Link](cont[i])
else:
[Link]()
OUTPUT:
Motherboard
Computer
Mouse
Speaker
12. Turtle Graphics

CODE:
import turtle
# Create turtle object
tur = [Link]()
# Set screen size and background
[Link](canvwidth=200, canvheight=100)
[Link]("blue")
# Draw pattern
for i in range(100):
[Link](10)
[Link](40)
[Link]()
13. Towers of Hanoi

CODE:
defTowerOfHanoi(n , source, destination, auxiliary):
if n==1:
print ("Move disk 1 from
source",source,"todestination",destination)
return
TowerOfHanoi(n-1, source, auxiliary, destination)
print ("Move
disk",n,"fromsource",source,"todestination",destination)
TowerOfHanoi(n-1, auxiliary, destination, source)
n=4
TowerOfHanoi(n,'A','B','C')
OUTPUT:
Move disk 1 from source A to destination C
Move disk 2 from source A to destination B
Move disk 1 from source C to destination B
Move disk 3 from source A to destination C
Move disk 1 from source B to destination A
Move disk 2 from source B to destination C
Move disk 1 from source A to destination C
Move disk 4 from source A to destination B
Move disk 1 from source C to destination B
Move disk 2 from source C to destination A
Move disk 1 from source B to destination A
Move disk 3 from source C to destination B
Move disk 1 from source A to destination C
Move disk 2 from source A to destination B
Move disk 1 from source C to destination B
14. Menu Driven Dictionary
CODE:
importjson
fromdifflib import get_close_matches
data = [Link](open("[Link]"))
def translate(w):
w = [Link]()
if w in data:
return data[w]
eliflen(get_close_matches(w, [Link]())>0:
yn = input("Did you mean % s instead? Enter Y if yes, or
N if no: " % get_close_matches(w, [Link]())[0])
yn = [Link]()
ifyn == "y":
return data[get_close_matches(w, [Link]())[0]]
elifyn == "n":
return "The word doesn't exist. Please double
check it."
else:
return "We didn't understand your entry."
else:
return "The word doesn't exist. Please double check it."
word = input("Enter word: ")
output = translate(word)
if type(output) == list:
for item in output:
print(item)
else:
print(output)
input('Press ENTER to exit')
OUTPUT:

:
15. HANGMAN GAME
CODE:
import random
name = input("What is your name? ")
print("Good Luck ! ", name)
words = ['rainbow', 'computer', 'science', 'programming',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']
word = [Link](words)
print("Guess the characters")
guesses = ''
turns = 12
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char, end=" ")
else:
print("_")
print(char, end=" ")
failed += 1
if failed == 0:
print("You Win")
print("The word is: ", word)
break
print()
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print("Wrong")
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Loose")
OUTPUT:
What is your name? sanjai
Good Luck !sanjai
Guess the characters
_
w_
a_
t_
e_
r
guess a character: water
w a t e r You Win
The word is: water

You might also like