Вы находитесь на странице: 1из 3

Practice Programs

1. Answer these three questions without typing code. Then type code to check your
answer.
• What is the value of the expression 4 * (6 + 5)? = 44
• What is the value of the expression 4 * 6 + 5? = 29
• What is the value of the expression 4 + 6 * 5? = 34
Solution:
x = 4*(6+5)
y = 4*6+5
z = 4+6*5
print(x)
print(y)
print(z)
2. What is the type of the result of the expression 3 + 1.5 + 4?

Solution:
x = 3+1.5+4
print(x)
print(type(x))

3. What would you use to find a number’s square root, as well as its Square?

Solution:
# First Method
x = int(input("Enter the Number which requires square & square root : "))
print("Square of given Number is: ",x**2)
print("Square root of given Number is: ",x**(1/2))
# Second Method
import math
x = int(input("Enter the Number which requires square & square root : "))
print("Square of given Number is: ",math.pow(x,2))
print("Square root of given Number is: ",math.sqrt(x))
4. Given the string 'hello', give an index command that returns ‘e’.
Solution:
s = "Hello"
print(s[1])
5. Reverse the string 'hello' using slicing.
Solution:
s = "Hello"
print(s[::-1])
6. Given the string ‘hello’, give two methods of producing the letter 'o' using indexing.
Solution:
s = "Hello"
print(s[4])
print(s[-1])
7. 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.)
Solution:
a = input("Enter the string : ")
b = a[::-1]
if a == b :
print("Given string is palindrom : ",a)
else:
print("Given string is not palindrom : ",a)

8. Go through the string below and if the length of a word is even or odd print that

st = 'Print every word in this sentence that has an even number of letters’
Solution:
a = "Print every word in this sentence that has an even number of letters"
b = len(a.split())
c = a.split()
for x in range(b):
d = len(c[x])
if d % 2 ==0:
print(c[x],"-Even")
else:
print(c[x],"-Odd")
9. 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:
i. Rock beats scissors
ii. Scissors beats paper
iii. Paper beats rock

10. 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.
Solution:
name = input("Enter the Name of the User")
age = int(input("Enter the Age of the User"))
by = 2020 - age
hy = by + 100
print(name, "will have 100 years old in ",hy)

Вам также может понравиться