pex.doc
pex.doc
Python Programming
Course Code: IT3ES03
Faculty of Engineering
Department of Information Technology
ENROLLMENT NO:-EN22IT301070
List of Programs
Answer these three questions without typing code. Then type code to check your answer.
1. • What is the value of the expression 4 * 6 + 5? • What is the value of the expression
4 + 6 * 5?
5. WAP to convert numbers from octal, binary and hexadecimal systems into decimal number
system?
6. What would you use to find a number’s square root, as well as its Square?
7. WAP to find the product of two numbers using command line arguments?
8. WAP to Given the string 'hello', give an index command that returns 'e’.
10. WAP to Given the string ‘hello’, give two methods of producing the letter 'o' using indexing.
11. WAP to Ask the user for a string and print out whether this string is a palindrome or not. (A
palindrome is a string that reads the same forwards and backwards.)
12. Go through the string below and if the length of a word is even, print"even!” st = 'Print every
word in this sentence that has an even number of letters’
13. Write a program that picks a random integer from 1 to 100, and has players guess the number.
The rules are:
iv. When the player's guess equals the number, tell them that they have guessed correctly
and how many guesses it took.
14. Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare
them, print out a message of congratulations to the winner, and ask if the
15. Create a program that asks the user to enter their name and their age. Print out a message
addressed to them that tells them the year that they will turn 100 years old.
16.
WAP to create a byte type array, read and display the elements of the array.
17. WAP to accept a numeric digit from the keyboard and display in words.
19. WAP to accept a number from the keyboard and test whether a number is even or odd.
20. WAP to test whether a given number is in between 1 and 10.
25. WAP to display the stars in an equilateral triangular form using a loop.
30. Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a
new list of only the first and last elements of the given list. For practice, write this code inside a
function
31. WAP to Use List comprehension to create a list of all numbers between 1 and 50 that are
divisible by 3
32. Take a list, say for example this one: a. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b. and write a
program that prints out all the elements of the list that are less than 5.
33. Take two lists, say for example these two: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4 5,
6, 7, 8, 9, 10, 11, 12, 13] and write a program that returns a list that contains only the elements
that are common between the lists (without duplicates). Make sure your
34. Write a program (function!) that takes a list and returns a new list that contains all the
elements of the first list minus all the duplicates.
35. Write a Python class named Circle constructed by a radius and two methods which will
compute the area and the perimeter of a circle.
36. Write a Python class named Rectangle constructed by a length and width and a method which
will compute the area of a rectangle.
37. Write a Python class to reverse a string word by word. • Input string: 'hello .py' • Expected
Output: '.py hello'
38. Given a .txt file that has a list of a bunch of names, count how many of each name there are in
the file, and print out the results to the screen.
40. Write a Python program to combine each line from first file with the corresponding line in
second file
PROGRAM 1
AIM: Answer these three equations without using code then write code to check your answer.
a) 4*(6+5)
CODE:
a= 4 * (6+5)
OUTPUT:
b)
4*6+5
CODE:
a= 4 * 6 + 5
CODE:
a =4+6*5
print("Value of a is: ", a)
PROGRAM 21
AIM: Write a program that picks a random integer from 1 to 100, and has players guess the
number. The rules are:
i. If a player guess is less than 1 or greater than 100, say ”OUT OF BOUNDS”.
b. Farther from the number than the previous guess, return COLDER”.
iv. When the player guess equals the number, tell them that they have guessed correctly
and how many guesses it took.
CODE:
PROGRAM 2
AIM: What is the type of the result of the expression -> 3 + 1.5 + 4? Using the Type() function
CODE:
x = 3 + 1.5 + 4
print(x)
print(type(x))
Output:-
PROGRAM 3
CODE:
sum=a+b
print(sum)
Output:-
PROGRAM 4
AIM: write a python program to print the sum of two complex numbers
CODE:
print("The sum of the two entered complex numbers is: ", sum)
Output:-
PROGRAM 5(A)
AIM: Write a python program to swap two numbers without a third variable.
CODE:
Output:-
OUTPUT:
PROGRAM 5(B)
AIM: Write a python program to swap two numbers using a third variable.
CODE:
temp = a
a=b
b = temp
Output:-
PROGRAM 6
AIM: Write a python program to give a string “hello” give an index command that return key
CODE:
string = "Hello!"
print(string.index("o"))
Output:-
PROGRAM 7
CODE:
print(String[::-1])
Output:-
PROGRAM 8
AIM: Write a program to give a string “Hello” and give two methods of producing the letter “o”
using indexing
CODE:
string = "Hello"
print(string[4])
print(string[-1])
Output:-
PROGRAM 9
CODE:
rev_string = string[::-1]
Output:-
PROGRAM 10
CODE:
Output:-
PROGRAM 11
Aim:-Write a program to check if a year is a leap year or not.
CODE:
year = int(input("Enter any year to check if its a leap year or not: "))
Output:-
PROGRAM 12
CODE:
import math
Output:-
PROGRAM 13
CODE:
Output:-
PROGRAM 14
CODE:
import cmath
D = (b ** 2) - (4 * a * c)
Output:-
PROGRAM 15
Aim:Write a program to find a square root of a number and also its square using import math.
CODE:
import math
num = int(input("Enter a number: "))
Output:-
PROGRAM 16
AIM: Write a program to display the name of the Month according to the number inputted by
the user.
CODE:
if num == 1:
print("January")
elif num == 2:
print("February")
elif num == 3:
print("March")
elif num == 4:
print("April")
elif num == 5:
print("May")
elif num == 6:
print("June")
elif num == 7:
print("July")
elif num == 8:
print("August")
elif num == 9:
print("September")
elif num == 10:
print("October")
elif num == 11:
print("November")
elif num == 12:
print("December")
else:
print("Enter a number between 1 and 12!")
Output:-
PROGRAM 17
AIM: Write a program to find the ASCII value of a character and vice versa.
CODE:
OutPut:-
PROGRAM 18
CODE:
Output:-
PROGRAM 19
CODE:
dec = 10
print("binary conversion: ", bin(dec))
print("octal conversion: ", oct(dec))
print("hexadecimal conversion: ", hex(dec))
binary = 0b0011
print("Decimal value: ", int(binary))
octal = 0o54
print("Decimal value: ", int(octal))
hexadecimal = 0xab
print("Decimal value: ", int(hexadecimal))
Output:-
PROGRAM 20
AIM: Go through the string below and if the length of a word is even, print ”even!” st = “Print
every word in this sentence that has an even number of letters”
Code:-
st = ["print", "every", "word", "in", "this", "sentence", "that", "has", "an", "even", "number",
"of",
"letters"]
Output:-
Program-21
AIM: Write a program that picks a random integer from 1 to 100, and has players guess the number.
The rules are:
i. If a player guess is less than 1 or greater than 100, say ”OUT OF BOUNDS”.
ii. On a player’s first turn, if their guess is
a. Within 10 of the number, return WARM!”.
b. Further than 10 away from the number, return COLD!”.
iii. On all subsequent turns, if a guess is
a. Closer to the number than the previous guess, return WARMER!”.
b. Farther from the number than the previous guess, return COLDER”.
iv. When the player guess equals the number, tell them that they have guessed correctly and how
many guesses it took.
Code:-
import random
# Initialize variables
i=0
count = 0
l = []
x = random.randint(1, 100) # Random number between 1 and 100
while i < 1:
count += 1
try:
a = int(input('Guess a number between 1 & 100: '))
except ValueError:
print("Please enter a valid integer.")
continue
l.append(a)
if a == x:
print("That's the correct number! Your number of guesses is:", count)
i=1
else:
if count == 1: # First guess
if abs(a - x) <= 10:
print('Warm')
else:
print('Cold')
else: # Subsequent guesses
if abs(a - x) > abs(l[-2] - x):
print('Colder')
else:
print('Warmer')
Output:-
PROGRAM 22
AIM: Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input),
compare them, print out a message of congratulations to the winner, and ask if the players
want to start a new game)
CODE:
if u1 == u2:
print("It's a tie!")
elif u1 == 'rock':
if u2 == 'scissors':
print("Rock wins!")
else:
print("Paper wins!")
elif u1 == 'scissors':
if u2 == 'paper':
print("Scissors win!")
else:
print("Rock wins!")
elif u1 == 'paper':
if u2 == 'rock':
print("Paper wins!")
else:
print("Scissors win!")
else:
print("Invalid input! You have not entered rock, paper, or scissors. Try again.")
Output:-
PROGRAM 23
Create a program that asks the user to enter their name and their age. Print out a message
addressed to them that tells them the year that they will turn 100 years old.
CODE:
Output:-
PROGRAM 24
AIM: WAP to create a byte type array, read and display the elements of the array.
CODE:
OUTPUT:
PROGRAM 25
CODE:
if num == 0:
print("Zero")
elif num == 1:
print("One")
elif num == 2:
print("Two")
elif num == 3:
print("Three")
elif num == 4:
print("Four")
elif num == 5:
print("Five")
elif num == 6:
print("Six")
elif num == 7:
print("Seven")
elif num == 8:
print("Eight")
elif num == 9:
print("Nine")
else:
print("Only single digit is supported")
Output:-
PROGRAM 26
Code:-
if num == 1:
print("Gold!")
elif num == 2:
print("Silver")
else:
print("Bronze")
Output:-
PROGRAM 27
CODE:
Output:-
PROGRAM 28
Code:-
Output:-
PROGRAM 29
Code:-
Output:-
PROGRAM 30
Code:-
for i in range(1, 11):
if i % 2 != 0:
print(i)
Output:-
PROGRAM 31
Code:-
list_of_numbers = []
max_size = int(input("Enter the maximum size of the list: "))
sum_of_elements = 0
Output:-
PROGRAM 32
Code:-
n=4
k=n-1
# Print stars
for j in range(0, i + 1):
print("* ", end="")
Output:-
PROGRAM 33
Code:-
Output:-
PROGRAM 34
CODE:
Output:-
PROGRAM 35
Code:-
Output:-
PROGRAM 36
CODE:-
z=0
a=1
for i in range(0, 10):
if i == 0:
print(z, end=" ")
elif i == 1:
print(a, end=" ")
else:
x=z
y=a
print(x + y, end=" ")
z=a
a=x+y
Output:-
PROGRAM 37
AIM: Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a
new list of only the first and last elements of the given list. For practice, write this code inside a
function
for i in range(n):
ele = int(input("Enter list element: "))
list2.append(ele)
if i == 0 or i == n - 1:
list1.append(ele)
fun()
Output:-
PROGRAM 38
AIM: WAP to Use List comprehension to create a list of all numbers between 1 and 50 that are
divisible by 3
Code:-
numbers = []
for i in range(51):
if i % 3 == 0:
numbers.append(i)
print(numbers)
Output:-
PROGRAM 39
Aim:-Take a list, say for example this one: a. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b. and write a
program that prints out all the elements of the list that are less than 5.
Code:-
Output:-
PROGRAM 40
AIM: Take two lists, say for example these two: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13] and write a program that returns a list that contains only the elements that
are common between the lists (without duplicates). Make sure your program works on two lists of
different sizes.
CODE:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = set(a) & set(b)
print(c)
Output:-