Basic data types
Questions
1. a=4
b='string'
print a+b
what is the error in the above program? Please correct the error.
Find out the output and its type of the following statements.
int(5.0)
float(5)
str(7)
int("")
str(7.0)
float("5.0")
int("five")
Write a program to check a character in a string.
Note: Take a string of your wish.
Consider you have 22 apples,20 bananas,30 carrots. Create variables for each fruit and print the output as follows,
I have 22 apples 20 bananas 30 carrots.
Note:In place of number use the variable.
Answer the following questions.
Which function used for the address of a variable?
Which function used to know whether a given variable is keyword?
Which function used to convert object into an expression string?
Which function used to find maximum size that integer can take?
Answers:
1.
cannot concatenate string with integer.
print str(a)+b
2.
Ans :
5 integer
5.0 floating point
7 string
error (invalid literal for int)
7.0 string
5.0 float
error
3.
#!/usr/bin/python
x="python"
ch='t'
print ch in x
Note: Output will be 'True' if character found otherwise 'False'
4.
#!/usr/bin/python
apple=22
banana=20
carrot=30
print "I have "+str(apple)+" apples "+str(banana)+" bananas "+str(carrot)+" carrots."
5.
id(variablename)
keyword.iskeyword(name)
repr(word)
sys.maxint
------------------------------------------------------------------------------------------------------------------
How user interact with the application? - I/O operations
Write a program to print absolute value of a given number.
Write a program to find the type of the given input.
Write a program to to take input and convert it into cube at input() function only and print the output.
Write a program to check whether given input is a prime number.
Write a program that prints output in the same line each value seperated by -.
like 1-4-5-g-6-8.0 e.t.c.,
---------------------------------------------------------------------------------------------------------------------------
Answers:
1.
#!/usr/bin/python
x=float(input('enter input'))
if x>=0:
print ("absolute value of", x ,"is",x)
else:
print ("absolute value of", x, "is",-x)
2.
#!/usr/bin/python
x=input('Enter input':)
print (type(x))
if type(x)==type(""):
print ("It is a string")
elif type(x)==type(1):
print ("It is an integer")
elif type(x)==type(1.0):
print ("It is a floating point")
else:
print ("It is other than integer,floating point and string")
3.
#!/usr/bin/python
x=(lambda x:x*x*x)(input('enter'))
print x
4.
#!/usr/bin/python
num = int(input("Enter a number: "))
if num > 1:
for i in range(2,num):
if (num % i) == 0:
s='False'
break
else:
s='True'
else:
print(num,"is not a positive number")
if s=='False':
print(num,"is not a prime number")
else:
print(num,"is a prime number")
5.
#!/usr/bin/python
li=[1,4,5,'g',6,8.0]
i=0
for k in li:
if i!=(len(li)-1):
print (k,end="-")
i+=1
else:
print (k)
-----------------------------------------------------------------------------------------------------
how to execute a block of statements repetitively – loop structures
Write a program to print even numbers less than 100
Write a program to print prime numbers less than 100 using for else statement
Write a program to print output as following
^ ^ ^ ^ ^
^ ^ ^ ^
^ ^ ^
^ ^
^
^ ^
^ ^ ^
^ ^ ^ ^
^ ^ ^ ^ ^
-----------------------------------------------------------------------------------------------------------------------
Write a program to find whether a given number is palindrome or not
Write a program to display multiplicaion table for a given number
Ex: number is 12
Then 12*1=12
12*2=24
and so on.
Answers:
1.
#!/usr/bin/python
for x in range(2,101,2):
print (x)
2.
#!/usr/bin/python
for num in range(1,101):
for i in range(2,num):
if (num%i==0):
break
else:
print(num)
break
3.
#!/usr/bin/python
for i in range(5,0,-1):
for j in range(0,i):
print '^',
print
for i in range(1,6):
for j in range(0,i):
print '^',
print
4.
#!/usr/bin/python
x=int(input('Enter number'))
z=""
p=x
while x!=0:
y=x%10
z+=str(y)
print (z)
x=int(x/10)
if str(p)==z:
print ("It is a palindrome")
else:
print ("It is not a palindrome")
5.
#!/usr/bin/python
x=int(input('enter number'))
for i in range(1,11):
print (x,"*",i,"=",x*i)
Questions
1. a=4
b='string'
print a+b
what is the error in the above program? Please correct the error.
Find out the output and its type of the following statements.
int(5.0)
float(5)
str(7)
int("")
str(7.0)
float("5.0")
int("five")
Write a program to check a character in a string.
Note: Take a string of your wish.
Consider you have 22 apples,20 bananas,30 carrots. Create variables for each fruit and print the output as follows,
I have 22 apples 20 bananas 30 carrots.
Note:In place of number use the variable.
Answer the following questions.
Which function used for the address of a variable?
Which function used to know whether a given variable is keyword?
Which function used to convert object into an expression string?
Which function used to find maximum size that integer can take?
Answers:
1.
cannot concatenate string with integer.
print str(a)+b
2.
Ans :
5 integer
5.0 floating point
7 string
error (invalid literal for int)
7.0 string
5.0 float
error
3.
#!/usr/bin/python
x="python"
ch='t'
print ch in x
Note: Output will be 'True' if character found otherwise 'False'
4.
#!/usr/bin/python
apple=22
banana=20
carrot=30
print "I have "+str(apple)+" apples "+str(banana)+" bananas "+str(carrot)+" carrots."
5.
id(variablename)
keyword.iskeyword(name)
repr(word)
sys.maxint
------------------------------------------------------------------------------------------------------------------
How user interact with the application? - I/O operations
Write a program to print absolute value of a given number.
Write a program to find the type of the given input.
Write a program to to take input and convert it into cube at input() function only and print the output.
Write a program to check whether given input is a prime number.
Write a program that prints output in the same line each value seperated by -.
like 1-4-5-g-6-8.0 e.t.c.,
---------------------------------------------------------------------------------------------------------------------------
Answers:
1.
#!/usr/bin/python
x=float(input('enter input'))
if x>=0:
print ("absolute value of", x ,"is",x)
else:
print ("absolute value of", x, "is",-x)
2.
#!/usr/bin/python
x=input('Enter input':)
print (type(x))
if type(x)==type(""):
print ("It is a string")
elif type(x)==type(1):
print ("It is an integer")
elif type(x)==type(1.0):
print ("It is a floating point")
else:
print ("It is other than integer,floating point and string")
3.
#!/usr/bin/python
x=(lambda x:x*x*x)(input('enter'))
print x
4.
#!/usr/bin/python
num = int(input("Enter a number: "))
if num > 1:
for i in range(2,num):
if (num % i) == 0:
s='False'
break
else:
s='True'
else:
print(num,"is not a positive number")
if s=='False':
print(num,"is not a prime number")
else:
print(num,"is a prime number")
5.
#!/usr/bin/python
li=[1,4,5,'g',6,8.0]
i=0
for k in li:
if i!=(len(li)-1):
print (k,end="-")
i+=1
else:
print (k)
-----------------------------------------------------------------------------------------------------
how to execute a block of statements repetitively – loop structures
Write a program to print even numbers less than 100
Write a program to print prime numbers less than 100 using for else statement
Write a program to print output as following
^ ^ ^ ^ ^
^ ^ ^ ^
^ ^ ^
^ ^
^
^ ^
^ ^ ^
^ ^ ^ ^
^ ^ ^ ^ ^
-----------------------------------------------------------------------------------------------------------------------
Write a program to find whether a given number is palindrome or not
Write a program to display multiplicaion table for a given number
Ex: number is 12
Then 12*1=12
12*2=24
and so on.
Answers:
1.
#!/usr/bin/python
for x in range(2,101,2):
print (x)
2.
#!/usr/bin/python
for num in range(1,101):
for i in range(2,num):
if (num%i==0):
break
else:
print(num)
break
3.
#!/usr/bin/python
for i in range(5,0,-1):
for j in range(0,i):
print '^',
for i in range(1,6):
for j in range(0,i):
print '^',
4.
#!/usr/bin/python
x=int(input('Enter number'))
z=""
p=x
while x!=0:
y=x%10
z+=str(y)
print (z)
x=int(x/10)
if str(p)==z:
print ("It is a palindrome")
else:
print ("It is not a palindrome")
5.
#!/usr/bin/python
x=int(input('enter number'))
for i in range(1,11):
print (x,"*",i,"=",x*i)
No comments:
Post a Comment