Variables and Printing Data in Python
In this section, we will learn how data is stored in a program
and how to print data on the screen.
Variable in Python
A variable is a name that refers to an object (value) stored in memory.
The value used in a program is stored in memory,
and the variable refers to that value.
This means the value can be accessed using the variable.
The value of a variable can change during program execution,
which is why it is called a variable.
Creating a Variable in Python
Syntax:
variable_name = value
Here
variable_name → name given to the variable that refers to the value (object) in memory
value → value (object) stored in memory
Example:
quantity = 10
Here
quantity → name of the variable that refers to the value 10 in memory
10 → value (object) stored in memory
Variable Representation in Memory
Rules for Naming Variables
- Allowed characters: A–Z, a–z, underscore (_), digits 0–9 (but cannot start with a digit).
- Minimum length: Variable name must have at least one character.
- No upper limit: There is no limit on variable name length.
- No special characters: Characters like @, $, %, -, space are not allowed.
- Case-sensitive: Age, age, and AGE are different variables.
- Unicode support: Unicode characters (non-ASCII) are also allowed (e.g., Telugu names).
Valid Variable Names
name = "NameXXXX" # starts with letters
student_name = "studentXXXX" # underscore allowed
age25 = 30 # digits allowed but not at the start
marks = 95 # meaningful name
పేరు = "పేరు xxxx" # Unicode (Telugu) is valid
Invalid Variable Names
25age = 30 # cannot start with digit
student-name = "studentXXXX" # hyphen (-) not allowed
my name = "NameXXXX" # space not allowed
class = "Test" # keyword not allowed
@value = 100 # special characters not allowed
The following program demonstrates creating variables in Python
student_name = "NameXXXX"
marks = 60
print(student_name)
print(marks)
Output
NameXXXX
60
NameXXXX
60
In the above program:
-
student_name = "NameXXXX"stores the string "NameXXXX" in the variablestudent_name. -
marks = 60stores the integer value 60 in the variablemarks. -
print(student_name)prints the value stored instudent_name. -
print(marks)prints the value stored inmarks.
Keywords in Python
Keywords in Python are built-in and predefined words.
These are reserved words in Python.
They cannot be used as variable names, function names, or class names.
Printing Data in Python
In Python, the
print() function is used to display data as output.
Syntax:
print(*objects, sep=' ', end='\n', file=None, flush=False)
Here
-
*objects
- Values to be printed (strings, numbers, variables).
- All values are internally converted to strings.
-
sep
- Separator between multiple values.
- Default is space (
' ').
-
end
- Specifies what is printed at the end.
- Default is newline (
\n).
-
file
- Specifies where output should be printed.
- Default is console (
sys.stdout).
-
flush
- Controls output buffer flushing.
- Default is False.
Escape Sequences in Python
Escape sequences in Python are special characters.
They are used to represent characters that cannot be written directly inside strings.
Example:
To print:
Writing
Instead, use escape characters:
To print:
This is "Python" LanguageWriting
print("This is "Python" Language") causes an error.Instead, use escape characters:
print("This is \"Python\" Language")
What We Have Learned
- Variables in Python
- Keywords in Python
- Printing Data in Python
- Escape Sequences in Python
- Numbers in Python will be covered in the next topic