Class 11 - Informatics Practices - Python - Variables

Q1: What are variables?

Answer: A variable is a name that is used to store data. It can be used to store different kinds of data.
e.g. age = 16

It defines a variable named 'age' and stores integer value as '16'. Thus 'age' is an integer data type variable.

Every variable in Python is 'dynamic type' i.e. you need not declare data type of a variable or declare a variable before using it. Every variable in Python is an object.

Q2: What are the rules to define a valid variable?

•  A variable can have a short name (e.g. x and y) or a long descriptive name (e.g. age, total_surface_area).
•  A variable name must start with a letter or the underscore character. Variables beginning with an underscore are generally reserved for special cases.
•  A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
•  Variable names are case-sensitive (age, Age and AGE are three different variables).
•  A variable name cannot be a reserved word.
•  Length of an Identifier is unlimited.

Q3: From the following, find out which assignment statement will produce an error. State reason(s) too.
(a) x =55
(b) y= 037
(c) z = 0o98
(d) 56thnumber = 3300
(e) length = 450.17
(f) !Taylor ='Instant'
(g) float= .17E - 03
(h) this variable = 87.E02
(i) FLOAT =0.17E - 03

(a) x =55  - Valid

(c) z = 0o98 - Invalid, A variable cannot start with number.

(d) 56thnumber = 3300 - Invalid, A variable cannot start with number.

(e) length = 450.17 - Valid lenght is a variable of float type.

(f) !Taylor ='Instant'
Invalid. Reason : Can not start with any special character. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

(g) float= .17E - 03
Invalid. 'float' is a keyword, though Python 3 accepts it but the stored value i.e. .17E - 03 is invalid. The valid value should be .17E-03.

(h) this variable = 87.E02
Invalid, cannot contain spaces.

(i) FLOAT =0.17E - 03
Invalid, the value cannot have blank. It should be FLOAT = 0.17E-03 which is equivalent to 0.017

Q4: What is the error in the following Python Program with one statement
print("My Name is : ",name)
Suggest a solution.

Answer: The above statement is trying to print the value of an undefined variable name.The solution to the problem is to first define the variable name before using it. e.g.
name = "Nitika"
print("My Name is : ",name)

Q5: What will be the output of the following code:
x,y=2,6
x,y=y,x+2
print(x,y)

Q6: Predict the output of the following:
x,y=7,2
x,y,x=x+1,y+3,x+10
print(x,y)

Q7: What do you understand by undefined variable in Python?

Answer: Any variable that is not assigned is undefined in python. A NameError will be raised if any such variable is called.

Q8: What is Dynamic Typing feature of Python?

Answer: In python we can make a variable point to a value of different type by reassigning it to a value of that type, this is called dynamic typing.
e.g.
name = 28
#name is defined as integer type variable storing 28
name = "eduvictors"
# name is reassigned as string data type variable

Q9: What is the error in following code : X , Y = 7 ?

Answer: The error is that the assignment of two variables is being done by providing just one value. A TypeError will be raised.

Q10: What would the following code do : X = Y = 7 ?

Answer: It will assign 7 to both X and Y.

Q11: Make change in the expression for z of previous question so that the output produced is zero. You cannot change the operators and order of variables. (Hint. Use a function around a sub-expression)
x, y = 4, 8
z = x/y*y
print(z)

Answer: The present code outputs 4.0 since x/y gives a float output.
Following snippet will give output as 0.
x, y = 4, 8
z = int(x/y)*y
print(z)

Q12: Following variable definition is creating problem X = 0281, give reasons.