Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Python and Mysql

This document contains Python programs to perform various tasks: find the greatest of three numbers, determine the day of the week from a number, print numbers 1-10 using a for loop, store employee data in a dictionary, count character frequencies in a string, print a number table, convert minutes to hours and minutes, convert between Celsius and Fahrenheit, use list operations like concatenation and membership checking, and use dictionary operations like accessing keys and values.

Uploaded by

Vanshika Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Python and Mysql

This document contains Python programs to perform various tasks: find the greatest of three numbers, determine the day of the week from a number, print numbers 1-10 using a for loop, store employee data in a dictionary, count character frequencies in a string, print a number table, convert minutes to hours and minutes, convert between Celsius and Fahrenheit, use list operations like concatenation and membership checking, and use dictionary operations like accessing keys and values.

Uploaded by

Vanshika Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Programs

• Find greatest of three given numbers


num1=int(input("enter number 1"))
num2=int(input("enter number 2"))
num3=int(input("enter number 3"))
if num1>num2 and num1>num3:
print("number 1 is greater")
elif num2>num1 and num2>num3:
print("number 2 is greater")
else:
print("number 3 is greater")

• Find day of the week


day = int(input("Enter day number"))
if day==1:
print("sunday")
elif day==2:
print("monday")
elif day==3:
print("tuesday")
elif day==4:
print("wednesday")
elif day==5:
print("thursday")
elif day==6:
print("friday")
elif day==7:
print("saturday")
else:
print("wrong choice")
• Print counting (1-10)
for num in range(1,11,1):
print(num)

• Write a program to enter names of employees and their


salaries as input and store them in a dictionary here n is to
input by the user.
num=int(input("enter the number of employees whose data to be stored:"))
count=1
employee=dict()
for count in range(4):
name= input("enter the name of the employee:")
salary=int(input("enter the salary:"))
employee[name]=salary
print("\n\nemployee_name\tsalary")
for k in employee:
print(k,'\t\t',employee[k])
• Write a program to count the number of times a character
appears in a given string.
st=input("enter a string:")
dic={}
for ch in st:
if ch in dic:
dic[ch]+=1
else:
dic[ch]=1
for key in dic:
print(key,':',dic[key])

• Print table of any number


num=int(input("Enter a number:"))
for i in range(1,11):
print(num, "x",i,"=",num*i)
• convert into time
time = int(input("Enter time in minutes: "))
hours = time / 60
minutes = time % 60
print("Hours are", hours)
print("Minutes are", minutes)

• Write a program to convert given temperature under degree


celcius and degree farenheit
C = float(input("Enter temperature in degrees Celsius: "))
F = (9.0/5) * C + 32
print("Temperature in degrees Fahrenheit:", F)
• Program to use all list operations(functions)
L1 = ["red", "blue"]
L2 = ["green", "yellow"]

# concatenation operator
print(L1 + L2)

# replication operator
print(L1 * 3)

# membership operator
print("yellow" in L1)
print("yellow" not in L1)

# Functions used in list


print(len(L1))
print(len(L2))
L1.append("purple")
print(L1)
L1.extend(L2)
print(L1)
L1.insert(0, "violet")
print(L1)
print(L1.index("purple"))
L1.sort()
print(L1)
L2.sort()
print(L2)

L3 = (14, 15, 5, 6, 8)
print(max(L3))
print(min(L3))
print(sum(L3))
• Write a program to use all dictionary functions
# define a dictionary
my_dict = {"apple": 3, "banana": 5, "orange": 2}

# access dictionary elements


print(my_dict["apple"])

# get list of dictionary keys


keys = list(my_dict.keys())
print(keys)

# get list of dictionary values


values = list(my_dict.values())
print(values)

# get list of key-value pairs (as tuples)


items = list(my_dict.items())
print(items)

# add new key-value pair


my_dict["grape"] = 4
print(my_dict)

# remove key-value pair


del my_dict["banana"]
print(my_dict)

# check if key exists in dictionary


print("apple" in my_dict)
print("banana" in my_dict)

# get length of dictionary


print(len(my_dict))
# clear dictionary
my_dict.clear()
print(my_dict)

You might also like