Page 121 - Trackpad_V1_Book 6_Flipbook
P. 121
DECLARING AND INITIALIZING A VARIABLE
In Python, variables are declared and initialized at the same time in the following way:
a = 10
b = 20
print (“a=”, a)
print (“b=”, b)
On the output screen, a = 10 and b = 20 will be printed. You can also assign the same value
to multiple variables at the time in the following way:
a = b = 20
print (“a=”, a)
print (“b=”, b)
On the output screen, a = 20 and b = 20 will be printed.
You can also assign multiple values to multiple variables in the same line in the following way:
name, age, Class= ”Anaya”, 12, ‘VII’
print (“Name is”, name)
print (“Age is”, age)
print (“Grade is”, Class)
On the output screen, Name is Anaya, Age is 12 and Grade is VII will be printed.
You must follow the given rules while creating and naming the variables:
A variable name must start with a letter or underscore character.
A variable name cannot start with a number.
A variable name can only contain alphanumeric characters (all the letters of the alphabet
and numbers) and underscores (_).
Variable names are case-sensitive.
Variable names cannot contain any special character or symbol.
DATA TYPES IN PYTHON
A data type is used to define the type of value a variable can contain. For example, a person’s
name must be stored as a string value whereas the person’s age must be stored as an integer.
Commonly used data types in Python are:
int: Positive or negative whole numbers (without any fractions), for example: a = 3
float: Any real number in which a fraction is denoted by a decimal symbol, for example: a = 3.5
string: A collection of one or more characters put in single or double-quotes, for example: a=”hello”
COMMENTS IN PYTHON
Comments in Python can be used to explain parts of the code. It can also be used to hide the code
as well. Python does not execute comments.
Introduction to Python 119

