Python Tutorials

Python Programs

Python Variables


Python Variables

In this tutorial, we will learn about variables in Python. We will cover the basics of declaring and using variables, including naming conventions.


What are Variables

Variables in Python are used to store data values that can be manipulated during program execution. Python is a dynamically typed language, meaning the data type of a variable can change at runtime.


Naming Variables

When naming variables in Python, follow these conventions:

  • Variable names must start with a letter or underscore (_).
  • Subsequent characters can include letters, digits, and underscores.
  • Variable names are case-sensitive.
  • Avoid using reserved keywords as variable names.
  • Use snake_case naming convention for readability.

Syntax

The syntax to declare variables in Python is simply assigning a value to a variable name:

variable_name = value


Variable storing Integer Value

  1. Declare a variable named num.
  2. Assign a value of 10 to the variable.
  3. Print the value of the variable.

Python Program

# Declare and initialize an integer variable
num = 10
# Print the value of the variable
print(f'The value of num is: {num}')

Output

The value of num is: 10


Variable storing String Value

  1. Declare a variable named name.
  2. Assign the value 'John' to the variable.
  3. Print the value of the variable.

Python Program

# Declare and initialize a string variable
name = 'John'
# Print the value of the variable
print(f'The value of name is: {name}')

Output

The value of name is: John


Variable storing Boolean Value

  1. Declare a variable named is_true.
  2. Assign the value True to the variable.
  3. Print the value of the variable.

Python Program

# Declare and initialize a boolean variable
is_true = True
# Print the value of the variable
print(f'The value of is_true is: {is_true}')

Output

The value of is_true is: True