Python Programming Language

Basics of Python

Python Variables - Python Tutorials

Python Variables

In any programming language, variables play a crucial role in storing and manipulating data. They act as containers that hold values, which can be numbers, strings (text), or more complex data structures.

Take a look at all the topics that are discussed in this article:

Python, a popular and versatile programming language, provides a straightforward and flexible way to work with variables.

Variable Naming Rules

  • Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
  • The remaining characters can be letters, numbers, or underscores.
  • Variable names are case-sensitive (e.g., myVar and myvar are different variables).

Creating Python Variables

In Python, a variable is created when you assign a value to it. Unlike some other programming languages, you don’t need to explicitly declare the data type of a variable in Python.

The Python interpreter automatically determines the type based on the assigned value. This flexibility makes Python code concise and easy to read. To create a variable, you use the assignment operator (=).

				
					x = 5
name = "Alice"
is_student = True
				
			

In the above code:

  • x is an integer variable with the value 5
  • name is a string variable with the value “Alice”
  • is_student is a boolean variable with the value True

Printing Python Variables

The most straightforward method for printing variables in Python is by using the built-in print() function. You can pass one or more variables as arguments to print() to display their values.

Example:

				
					name = "Alice"
age = 25
print(name, age)  # Output: Alice 25
				
			

Deleting Python Variables

In Python, variables can be deleted using the del statement. The del statement removes a variable, freeing up the memory it occupies.

It’s important to note that deleting a variable doesn’t delete the object it refers to; rather, it removes the variable name from the namespace, allowing the garbage collector to reclaim the memory if there are no other references to the object.

Example:

				
					x = 10
print(x)  # Output: 10

# Deleting the variable 'x'
del x

# Attempting to print 'x' after deletion will result in an error
# Uncommenting the line below would raise a NameError: name 'x' is not defined
# print(x)

				
			

Multiple Assignment Variable

You can assign values to multiple variables in a single line.

Example:

				
					a, b, c = 1, 2, 3

				
			

Getting Type of a Variable

In Python, you can determine the type of a variable using the built-in type() function. The type() function returns the type of the given object.

Here’s how you can use it:

				
					# Define variables of different types
integer_var = 42
float_var = 3.14
string_var = "Hello, Python!"
list_var = [1, 2, 3]
dict_var = {'a': 1, 'b': 2}

# Use the type() function to get the type of each variable
print("Type of integer_var:", type(integer_var))
print("Type of float_var:", type(float_var))
print("Type of string_var:", type(string_var))
print("Type of list_var:", type(list_var))
print("Type of dict_var:", type(dict_var))

				
			

Case-Sensitivity of Python Variables

Python is a case-sensitive programming language, which means that it distinguishes between uppercase and lowercase letters in variable names. This sensitivity extends to all identifiers, including variables, functions, classes, and module names.

Example:

				
					my_variable = 42
My_Variable = "Hello, Python!"

print(my_variable)    # Output: 42
print(My_Variable)    # Output: Hello, Python!

				
			

Variables in Python are case-sensitive, so my_variable and My_Variable would be considered as two different variables.

Understanding and effectively using variables is fundamental to programming in Python. They enable you to store and manipulate data, making your code more dynamic and adaptable.

Keep practicing and exploring Python’s capabilities, and don’t hesitate to dive deeper into more advanced topics as you continue your learning journey.

Categories