0% found this document useful (0 votes)
6 views7 pages

Python Assignment 1

The document contains a series of Python programming exercises covering various topics such as user input, arithmetic operations, relational operators, loops, functions, data structures (lists, tuples, dictionaries, sets), and file handling. Each exercise includes code snippets that demonstrate how to implement specific functionalities, such as calculating averages, finding the greatest number, generating patterns, and manipulating data structures. The exercises are designed to help users practice and improve their Python programming skills.

Uploaded by

k1m18a14
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)
6 views7 pages

Python Assignment 1

The document contains a series of Python programming exercises covering various topics such as user input, arithmetic operations, relational operators, loops, functions, data structures (lists, tuples, dictionaries, sets), and file handling. Each exercise includes code snippets that demonstrate how to implement specific functionalities, such as calculating averages, finding the greatest number, generating patterns, and manipulating data structures. The exercises are designed to help users practice and improve their Python programming skills.

Uploaded by

k1m18a14
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/ 7

# Write a Python program that accepts a user's full name and age,

# then displays a "Welcome, (name)! You are (age) years old"


message using formatted strings.
# Also, print the data types of both inputs

name = input("Enter your full name: ")


age = int(input("Enter your age: "))
print(f"Welcome,{ name}! You are {age} years old.")
print(f"Data type of name: {type(name)}")
print(f"Data type of age: {type(age)}")

# Write a Python program that demonstrates arithmetic operations


with type conversion using input from the user.

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


b = int(input("Enter the second number: "))

print(f"Addition: {a + b}")
print(f"Subtraction: {a - b}")
print(f"Multiplication: {a * b}")
print(f"Division: {a / b}")

# Write a program that demonstrates the use of relational


operators.

a = int(input("Enter a: "))
b = int(input("Enter b: "))
print(f"{a} > {b} =",a > b)
print(f"{a} < {b} =",a < b)
print(f"{a} == {b} =",a == b)
print(f"{a} != {b} =",a != b)
print(f"{a} >= {b} =",a >= b)
print(f"{a} <= {b} =",a <= b)
# Write a program to take input from the user and calculate average of two numbers.
# Print their deviation

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


b = int(input("Enter second number: "))
avg = (a + b)/2
dev1 = avg - a
dev2 = avg - b
print(f"Average of {a} and {b} = {avg}")
print("deviation of first number =", dev1)
print("deviation of second number =", dev2)

# Write a program to find the greatest number among four numbers

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


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
d = int(input("Enter fourth number: "))

if a >= b and a >= c and a >= d:


print(f"The greatest number is: {a}")
elif b >= a and b >= c and b >= d:
print(f"The greatest number is: {b}")
elif c >= a and c >= b and c >= d:
print(f"The greatest number is: {c}")
else:
print(f"The greatest number is: {d}")

# Write a Python program using a for loop to display all numbers divisible by 7 but not by 5
between 100 and 200.

print("Numbers divisible by 7 but not by 5 between 100 and 200:")


for i in range(100, 201):
if i % 7 == 0 and i % 5 != 0:
print(i, end=' ')

# Write a Python script that prints the Fibonacci sequence up to a given number using a while
loop

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


a = 0
b = 1
print(f"Fibonacci sequence up to {n}:")
while a <= n:
print(a, end=' ')
a, b = b, a + b
# Write a program to print the following pattern using nested loops:
# *
# * *
# * * *
# * * * *

n = int(input("Enter the number: "))


for i in range(1, n + 1):
for j in range(n - i):
print(" ", end="")
for k in range(i):
print("* ", end="")
print()

# OR

for i in range(1, n + 1):


print(" " * (n - i) + "* " * i)

# Write a program to print the following pattern using nested loops:


# 1
# 2 3
# 4 5 6
# 7 8 9 10
# 11 12 13 14 15

n = int(input("Enter the number: "))


count = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(count, end=" ")
count += 1
print()

# Write a program using functions and return statement to check whether a number is even or
odd
def even_odd(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"

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


print(f"Given number {a} is {even_odd(a)}")
# Write a Python script to count the frequency of each vowel in a given string.

a = input("Enter a word or sentence: ").lower()


v = "aeoiu"

# Write a program to check if the substring is present in a given string or not.(use regular
expressions)

import re
a = input("enter a word or sentence: ")
b = input("enter a word which you want to search: ")

if re.search(b, a):print("word is found")


else: print("word is not found")

# Create a Python list of 10 random integers.


# Then remove all duplicates and sort the list in descending order

a = [4 , 3, 8 , 3 , 4 , 8 , 2 , 1 , 5 , 3]
a.sort()
print(a)
# or
print(list(set(a)))

# Write a program that takes a list of numbers and


# prints a new list with the squares of all even numbers using list comprehension.

n = [4, 3, 9, 2, 8, 5, 6, 1, 7, 10]

s = [x**2 for x in n if x % 2 == 0]
s.sort()
print(s)
# Write a Python program to count occurrences of each item in a list and
# display the result as a dictionary.
subject = ['math', 'chemistry', 'python', 'math', 'webdev', 'python', 'chemistry', 'webdev',
'math', 'python']

count = {}

for i in subject :
if i in count:
count[i] += 1
else:
count[i] = 1

print(count)

# Write a Python program to reverse a list using slicing and print the result.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(a[::-1])

# Write a Python program to merge two lists and remove duplicates from the merged list.

a = [41, 42, 43, 44, 45, 46]


b = [45, 46, 47, 48, 49, 50]

print(list(set(a + b)))

# Create a tuple of 5 subjects and print each subject using a loop.

a = ('math', 'chemistry', 'python', 'webdev', 'communicative english')

for i in a:print(i)

# Create a tuple with at least 5 numbers and find the maximum and
# minimum value without using built-in functions

a = (45, 47, 49, 43, 48, 50)

a = list(a)
a.sort()
print("Maximum:", a[-1])
print("Minimum:", a[0])
# Write a program to store information of 3 students using tuples (name, roll, marks).
# Convert it into a set and display all unique records

s1 = ('viraj', 41, 75)


s2 = ('sumit', 46, 99)
s3 = ('arbaz', 21, 99)

print(s1, s2, s3)


print(set([s1, s2, s3]))

# Write a Python program to check if an element exists in a tuple.

a = (7, 3, 5, 9, 1, 4, 6)
b = int(input("Enter the number to check: "))

if b in a:print("found")
else:print("not found")

# Write a Python program to remove duplicates from a list using set and print the sorted
result.

a = [4, 3, 8, 3, 4, 8, 2, 1, 5, 3, 6, 4, 7, 9, 10]

print(set(a))

# Write a Python program to find the union, intersection, and difference of two sets.

a = {41, 42, 43, 44, 45, 46}


b = {45, 46, 47, 48, 49, 50}

print(a.union(b))
print(a.intersection(b))
print(a.difference(b))

# Write a Python program to take a sentence from the user and


# create a dictionary that stores each word and its length.
a = input("Enter a sentancce: ").split()

print(len(a))
#Write a Python program to convert a list of tuples (name, age) into a dictionary.

a = [('viraj', 19), ('sumit', 20), ('arbaz', 21)]

b = dict(a)
print(b)

# Write a Python program to merge two dictionaries.


# If keys are the same, sum the values.

# Create a dictionary with student names as keys and marks as values.


# Display the average marks

# Create a Python program to read a text file and count the number of lines, words, and
characters in it.

# Write a Python script that reads from one file, converts all text to lowercase, and writes
it to another file.

You might also like