0% found this document useful (0 votes)
11 views44 pages

pex.doc

The document outlines a Python programming course at Medi-Caps University, detailing various programming exercises and projects. It includes tasks such as calculating expressions, creating games, and manipulating strings and numbers. Additionally, it covers object-oriented programming concepts and file handling 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)
11 views44 pages

pex.doc

The document outlines a Python programming course at Medi-Caps University, detailing various programming exercises and projects. It includes tasks such as calculating expressions, creating games, and manipulating strings and numbers. Additionally, it covers object-oriented programming concepts and file handling 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
You are on page 1/ 44

MEDI-CAPS UNIVERSITY, INDORE

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.

• What is the value of the expression 4 * (6 + 5)?

1. • What is the value of the expression 4 * 6 + 5? • What is the value of the expression
4 + 6 * 5?

2. What is the type of the result of the expression 3 + 1.5 + 4?

3. Write a Python Program addition of two numbers?

4. Write a Python Program to display the sum of two complex numbers?

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’.

9. WAP to Reverse the string 'hello' using slicing.

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:

i. If a player's 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'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

players want to start a new game) Remember the rules:

• Rock beats scissors

• Scissors beats paper

• Paper beats rock

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.

18. WAP to display a group of messages when the condition is true?

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.

21. WAP to display even numbers between m and n

22. WAP to display characters of a string using for loops

23. WAP to display odd numbers from 1 to 10 using range ().


24. WAP to display and sum of a list of numbers using a loop.

25. WAP to display the stars in an equilateral triangular form using a loop.

26. WAP to display numbers from 1 to 100 in a proper format

27. WAP to search for an element in the list of elements.

28. WAP to display prime number series.

29. WAP to generate Fibonacci number series.

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

program works on two lists of different sizes.

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.

39. Write a Python program to remove newline characters from a file

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)

print("Value of a is: ", a)

OUTPUT:

b)

4*6+5

CODE:

a= 4 * 6 + 5

print("Value of a is: ", a)


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”.

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:
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

AIM: write a python program to add two numbers

CODE:

a = int(input("Enter 1nd num: "))

b = int(input("Enter 2nd num: "))

sum=a+b

print(sum)

Output:-

PROGRAM 4

AIM: write a python program to print the sum of two complex numbers

CODE:

i1 = complex(input("Enter the first complex number: "))

i2 = complex(input("Enter the second complex number: ")


) sum = i1 + i2

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:

num1 = int(input("Enter num1: "))

num2 = int(input("Enter num2: "))

print("Numbers before swapping: ", num1, num2)

num1, num2 = num2, num1

print("Numbers after swapping: ", num1, num2)

Output:-

OUTPUT:
PROGRAM 5(B)

AIM: Write a python program to swap two numbers using a third variable.

CODE:

a = int(input("Enter a number: "))

b = int(input("Enter another number: "))

print("Numbers before swapping: ", a, b)

temp = a

a=b

b = temp

print("Numbers after swapping: ", a, b)

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

AIM: Write a program to input a string and then reverse it.

CODE:

String = input("Enter any string: ")

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

Aim:-Input a string and check if its a palindrome or not

CODE:

string = input("Enter a string to check if its a Palindrome or not: ")

rev_string = string[::-1]

if string == rev_string: print("Palindrome")

else: print("Not a palindrome")

Output:-
PROGRAM 10

AIM: Write a program to check if a number is even or odd.

CODE:

num = int(input("Enter a number to check if its odd or even: "))

if num % 2 == 0:print("Its even!")

else: print("Its odd")

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: "))

if year % 4 == 0: print("Leap year")

else: print("Not a leap year")

Output:-

PROGRAM 12

AIM: Write a program to find the area and circumference of a circle.

CODE:

import math

radius = int(input("Enter the radius of circle: "))

circumference = 2 * 22/7 * radius

area = 22/7 * radius ** 2

print("Area of circle will be: ", area)

print("Circumference of circle will be: ", circumference)

Output:-
PROGRAM 13

Aim:-Write a program to convert degree Celsius into Fahrenheit

CODE:

Celsius = float(input("Enter a temperature in degree Celsius: "))

Fahrenheit = (Celsius * 9/5) + 32

print("The value in Fahrenheit is: ", Fahrenheit)

Output:-
PROGRAM 14

AIM: Write a program to find the solutions of quadratic Equations in python.

CODE:

import cmath

a = int(input("Enter 1st Coefficient: "))

b = int(input("Enter 2nd Coefficient: "))


c = int(input("Enter 3rd Coefficient: "))

D = (b ** 2) - (4 * a * c)

sol1 = (-b + cmath.sqrt(D)) / (2 * a)

sol2 = (-b - cmath.sqrt(D)) / (2 * a)

print("x1 = ", sol1)

print("x2 = ", sol2)

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: "))

print("Square of the number is: ", num ** 2)

print("Square root of the number is: ", math.sqrt(num))

Output:-

PROGRAM 16

AIM: Write a program to display the name of the Month according to the number inputted by
the user.

CODE:

num = int(input("Enter a number between 1-12: "))

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:

char = input("Enter a char to find its ASCII value: ")

print("ASCII value is: ", ord(char))

num = int(input("Enter a number to get its corresponding char value: "))

print("Char for the inputted ASCII value is: ", chr(num))

OutPut:-
PROGRAM 18

AIM: Find the area of triangle using import math

CODE:

height = float(input("Enter height of triangle: "))

base = float(input("Enter base of triangle: "))

area = 0.5 * base * height

print("Area of triangle is: ", area)

Output:-
PROGRAM 19

AIM: Convert binary, octal, hexadecimal, to decimal.

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"]

for i in range(0, 14):


if len(st[i]) % 2 == 0:
print(st[i])
else:
pass

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 < 1 or a > 100:


print('OUT OF BOUNDS')
continue

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)

Remember the rules:

• Rock beats scissors

• Scissors beats paper

• Paper beats rock

CODE:

u1 = input("Player 1, do you want to choose rock, paper, or scissors? ").lower()


u2 = input("Player 2, do you want to choose rock, paper, or scissors? ").lower()

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:

name = input("What is your name?")


age = int(input("And what is your age?"))
x = 100 - age
year = x + 2023
print(name, "You will turn 100 years old in the year: ", year)

Output:-
PROGRAM 24

AIM: WAP to create a byte type array, read and display the elements of the array.

CODE:

max = int(input("Enter max size of array: "))


array = []
for i in range(0, max):
ele = int(input())
array.append(ele)
byte_array = bytearray(array)
print(byte_array)

OUTPUT:
PROGRAM 25

Aim:-WAP to accept a numeric digit from keyboard and display in words.

CODE:

num = int(input("Enter a number: "))

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

Aim:WAP to display a group of messages when the condition is true?

Code:-

num = int(input("Enter your position: "))

if num == 1:
print("Gold!")
elif num == 2:
print("Silver")
else:
print("Bronze")

Output:-

PROGRAM 27

AIM: WAP to test whether a given number is in between 1 and 10.

CODE:

num = int(input("Enter a number: "))

if 0 < num < 11:


print("Yes, the number is between 0 and 10.")
else:
print("No, the entered number is not between 0 and 10.")

Output:-

PROGRAM 28

Aim:-WAP to display even numbers between m and n

Code:-

m = int(input("Enter the lower limit: "))


n = int(input("Enter the higher limit: "))

for i in range(m, n):


if i == 0:
continue
elif i % 2 == 0:
print(i)

Output:-
PROGRAM 29

AIM: WAP to display characters of a string using for loops

Code:-

string = input("Enter a string:")


for i in range(0, len(string)):
print(string[i])

Output:-

PROGRAM 30

Aim:WAP to display odd numbers from 1 to 10 using range ().

Code:-
for i in range(1, 11):
if i % 2 != 0:
print(i)

Output:-
PROGRAM 31

AIM: WAP to display and sum of a list of numbers using a loop.

Code:-

list_of_numbers = []
max_size = int(input("Enter the maximum size of the list: "))
sum_of_elements = 0

# Loop to get elements from the user


for i in range(max_size):
ele = int(input(f"Enter element {i + 1}: "))
list_of_numbers.append(ele)
sum_of_elements += ele

# Print the entered list and the sum


print("The entered list is:", list_of_numbers)
print("The sum is:", sum_of_elements)

Output:-

PROGRAM 32

Aim:-WAP to display the stars in an equilateral triangular form using a loop.

Code:-

n=4
k=n-1

for i in range(0, n):


# Print leading spaces
for j in range(0, k):
print(" ", end="")
k -= 1

# Print stars
for j in range(0, i + 1):
print("* ", end="")

# Move to the next line


print()

Output:-

PROGRAM 33

AIM: WAP to display numbers from 1 to 100 in a proper format

Code:-

for i in range(0, 60):


print(i, end=' ')

Output:-

PROGRAM 34

AIM: WAP to search for an element in the list of elements

CODE:

my_list = [20, 42, 98, 39]


ele = int(input("Enter an element to search in the list: "))
found = False
for i in range(len(my_list)):
if ele == my_list[i]:
print("Element found!", ele)
print("Location:", i + 1)
found = True
break
if not found:
print("Element not found in the list.")

Output:-

PROGRAM 35

AIM: WAP to display prime number series

Code:-

for num in range(0, 100):


if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=' ')

Output:-
PROGRAM 36

Aim:-WAP to generate Fibonacci number series.

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

Code:- def fun():


list2 = []
list1 = []
n = int(input("Enter the number of elements in the list: "))

for i in range(n):
ele = int(input("Enter list element: "))
list2.append(ele)

if i == 0 or i == n - 1:
list1.append(ele)

print("The entered list was:", list2)


print("Needed list:", list1)

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:-

numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]


for i in range(len(numbers)):
if numbers[i] < 5:
print(i)

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:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy