Python Exercise1

1.Write a program to take input of name and age from the user and print it

Eg: Your name is Ganesh, you are 10 years old

 

name = input(“Enter your name : “)

age =  input(“Enter your age : “)

print(“Your name is “,name,”, you are “,age,”Years old”)

 

2.Find the length of your name and print it as

Length of your name is : 6

print(“Length of your name is : “,len(name))

 

3.Create five valid variables and five invalid variables

 

a=10

_b=20

tup1=40

tup_1=70

__tup3=”test”

 

#invalid variables

#1a=20

#%b=90

#n ame=”Ujwal”

#20=”test”

#30=40

 

4.Print the type of name and age

name=input(“Enter your name:”)

age=input(“Enter your age:”)


print(“type(name) :”,type(name))

print(“type(age) :”,type(age))

 

5. Repeat name for 10 times

 

print(name*10)

 

6.Write a program to concatenate two strings

 

print(“New” + “Delhi”)

 

7.Write a program to concatenate a string and an integer value

 

print(“Population – ” + str(100000))

 

8.Enter three numbers (two integer values and one float value). Use different operators on those numbers and print the results.

Use: +,-,*,/,//,%,**

 

a=int(input(“Enter a integer value : “))

b=int(input(“Enter a integer value : “))

c=float(input(“Enter a Float Value : “))

 

print(“a+b”,a+b)

print(“a-b”,a-b)

print(“a*b”,a*b)

print(“a/c”,a/c)

print(“a//c”,a//c)

print(“a%b”,a%b)

print(“a**b”,a**b)

 

9.Write a program to convert 99.99 to int

 

print(int(99.99))

 

10. Write a program to take input of three values and print the largest and smallest value among three numbers

a=int(input(“Enter First  Value : “))

b=int(input(“Enter Second Value : “))

c=int(input(“Enter Third Value : “))

print(“Largest Value : “, max(a,b,c))

print(“Smallest Value : “, min(a,b,c))

 

11. Create a string “python is a High-level programming language”. Write a program to

  • Print all the characters in the string to capital letters
  • Print all the characters in the string to lower case
  • Capitalize the string and print it
  • Print the given string in to title case
  • Print whether “level” exists in the string or not

s = “python is a High-level programming language”

print(“UpperCase: “,s.upper())

print(“LowerCase: “,s.lower())

print(“Capitalize: “,s.capitalize())

print(“SwapCase: “,s.swapcase())

print(“Title: “,s.title())

 

12. Create a string “The Science of today is technology of tomorrow”. Write a program to find number of times character ‘t’ has been repeated”.

sen = “The Science of today is technology of tomorrow”

print(sen.count(‘t’))

13. Create a string “Peace begins with a smile”. Write a program to:

  • Find what character is at the position 10
  • Slice the string ‘smile’ from the given string
  • Print the last character of the given string
  • Print the string from ‘begin’ to end of the string
  • Extract the string ‘Pcbi tame’ from the given string
  • Reverse of a string
  • Print the string ‘Peace begins’
  • Print the index of ‘mile’
  • Print the complete string using indexing
  • Print last five characters from the string

 

st = “Peace begins with a smile”
print(“Character at position 10: “,st[9])
#print(st.index(‘smile’))
print(st[20:]) #(or)st[-5:]
print(st[-1])
print(st[6:])
print(st[::3])
print(st[::-1])
print(st[:12])
print(st.index(‘mile’))
print(st[:])
print(st[-5:])

14. Write a program to reverse a given string

 

name=input(“Enter your name: “)

print(name[::-1])

 

15. Write a program to read a number n and compute n+nn+nnn

Eg: If entered number is 4 –>4+44+444 —> result should be 492

 

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

temp=str(n)                     #Converting the input value to string

t1=temp+temp                    #concatenating the string result t1=44

t2=t1+temp                      #concatenating t1 and temp t2=44+444

comp=n+int(t1)+int(t2)          #Adding all the variables by converting t1 and t2 to int

print(“The value is:”,comp)

 

16. Write a program to reverse a number, and print the type of the reversed number

 

n=(input(“Enter a number n: “))

n=int((n[::-1]))

print(n,type(n))

 

17. Find the length of an integer

#It is not possible to find the length of an integer

 

Back to Main Page

Visit www.mongofactory.com to know more about the trainer.

Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.