You are on page 1of 3

Python Programming

Basics of Python:

1. Write a Python program to print the following string in a specific format (see the output).
Sample String : "Twinkle, twinkle, little star, How I wonder what you are! Up above the world
so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are"
Output :
Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are

Answer:

print("Twinkle, twinkle, little star, \n\tHow I wonder what you are! \n\t\tUp above the world
so high, \n\t\tLike a diamond in the sky. \nTwinkle, twinkle, little star, \n\tHow I wonder what
you are!")

2. Write a Python program to display the current date and time.

Answer:

import datetime
print(datetime.datetime.now())

3. Write a Python program that calculates the area of a circle based on the radius entered by
the user.
Sample Output :
r = 1.1
Area = 3.8013271108436504
Answer:
from math import pi
r = float(input ("Input the radius of the circle : "))
print ("The area of the circle with radius " + str(r) + " is: " + str(pi * r**2))

5. Write a Python program that accepts the user's first and


last name and prints them in reverse order with a space
between them.

Answer:
fname = input("Input your First Name : ")
lname = input("Input your Last Name : ")

Computer Age | Sandip Majhi


print ("Hello " + lname + " " + fname)

6. Write a Python program that accepts a sequence of comma-


separated numbers from the user and generate a list and a
tuple of those numbers.
Sample data : 3, 5, 7, 23
Output :
List : ['3', ' 5', ' 7', ' 23']
Tuple : ('3', ' 5', ' 7', ' 23')

Answer:
values = input("Input some comma seprated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)

7. Write a Python program to sum all the items in a list.

Answer:

def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers

print(sum_list([1,2,-8]))

8. Write a Python program to multiply all the items in a list.

Answer:
def multiply_list(items):
tot = 1
for x in items:
tot *= x
return tot
print(multiply_list([1,2,-8]))

9. Write a Python program to get the largest number from a


list.

Answer:

def max_num_in_list( list ):


max = list[ 0 ]

Computer Age | Sandip Majhi


for a in list:
if a > max:
max = a
return max
print(max_num_in_list([1, 2, -8, 0]))

Computer Age | Sandip Majhi

You might also like