Professional Documents
Culture Documents
04VARIABLES
04VARIABLES
04VARIABLES
VARIABLES
Variable is the name which we give to the memory What do you store in the variable?
location which holds some data. With a variable we
can store the data, access the data and also Each and every variable in a program holds value of
manipulate the data. some type which can be accessed or modified during
the due flow of the program.
PS. Variable is a container where you can store a
value. 4. Location:
How can I look for my variables? What if we want to submit value of a variable
To see your variable use print() function outside our code like questionnaire?
Ex. 4.2 ( Print a variable ) input() is a function that can accept values from a
user (you).
>>> print(age)
Syntax to submit value of a variable:
21
v = input(“Ask user to enter a value”)
What is print() function?
Ex. 4.5 ( Take input from a user )
print() is a function that can print the specified
age = input(“Enter your age? ”)
message to your computer screen.
print(“Your age is ”,age)
Syntax to print a message / text:
print(“write your message here”) Enter your age?
My age is 21
Invalid cases for variables
EXERCISE 4.1
While defining variables in python,
Create a variable "date" and store today's date then
print it? Variable names should be on the left side.
Value should be on the right side.
Hint: Output will be like below Violating this will result in syntax error.
Today's date is 6 Ex. 4.6 ( Creating a variable in wrong direction )
>>> 17 = age
Rules for creating a variable All 33 keywords in python contain only alphabet
symbols. All of them are in lower case except True,
1. must start with letter or underscore False, and None.
Ex. 4.7 ( Creating a variable with underscore ) To see all the keywords –
>>> My_age = 23 >>> import keyword
SyntaxError: invalid syntax >>> keyword.kwlist
2. cannot start with number [‘False’, ‘None’, ‘True’, ‘and’, ‘as’,
Ex. 4.8 ( Creating a variable starts with number ) ‘assert’, ‘break’, ‘class’,
‘continue’, ‘def’, ‘del’, ‘elif’,
>>> 1dollar = 76 ‘else’, ‘except’, ‘finally’, ‘for’,
‘from’, ‘global’, ‘if’, ‘import’,
SyntaxError: invalid syntax
‘in’, ‘is’, ‘lambda’, ‘nonlocal’,
3. cannot use any special symbol ‘not’, ‘or’, ‘pass’, ‘raise’,
‘return’, ‘try’, ‘while’, ‘with’,
Ex. 4.9 ( Creating a variable with special symbol )
‘yield’]
>>> $dollar = 74
PS. You cannot use these keywords as a variable,
SyntaxError: invalid syntax else will result in error.
4. can’t use space ( instead use underscore ) Ex. 4.12 ( Creating a variable with keyword name )
Ex. 4.16 ( Assign single value to multiple variables) print("In 2020, $1 = ",dollar)