CodeSutraHub

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

variable

Rules for Naming Variables

  1. Allowed characters: A–Z, a–z, underscore (_), digits 0–9 (but cannot start with a digit).
  2. Minimum length: Variable name must have at least one character.
  3. No upper limit: There is no limit on variable name length.
  4. No special characters: Characters like @, $, %, -, space are not allowed.
  5. Case-sensitive: Age, age, and AGE are different variables.
  6. 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
In the above program:
  • student_name = "NameXXXX" stores the string "NameXXXX" in the variable student_name.
  • marks = 60 stores the integer value 60 in the variable marks.
  • print(student_name) prints the value stored in student_name.
  • print(marks) prints the value stored in marks.

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
  1. *objects
    • Values to be printed (strings, numbers, variables).
    • All values are internally converted to strings.
  2. sep
    • Separator between multiple values.
    • Default is space (' ').
  3. end
    • Specifies what is printed at the end.
    • Default is newline (\n).
  4. file
    • Specifies where output should be printed.
    • Default is console (sys.stdout).
  5. 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: This is "Python" Language

Writing 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

Watch the following Video for Variables and Printing Data in Python