Search This Blog

Tuesday 7 March 2017

python solutions basic datatypes

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

No comments:

Post a Comment

Hadoop Analytics

NewolympicData

  Alison Bartosik 21 United States 2004 08-29-04 Synchronized Swimming 0 0 2 2 Anastasiya Davydova 21 Russia 2004 0...