Sitemap

Variables in {Python}

2 min readJan 23, 2023

--

Press enter or click to view image in full size
Python Variables

Variables are a fundamental concept in programming, and Python is no exception. In Python, a variable is a named location in memory where a value can be stored and retrieved later. Variables are used to store data that can be manipulated, processed, or used to control the flow of a program.

In Python, variables are defined using the assignment operator (=), which assigns a value to a variable. For example, the following code defines a variable called “x” and assigns it the value of 5:

x = 5

Once a variable has been defined, its value can be accessed by referencing the variable name. For example, the following code prints the value of “x” to the console:

print(x) # Output: 5

Please note that every variable in python is a Object, so if you try to print the type of any variable using the function type; it prints out the corresponding class.

print(type(x)) # Output: <class 'int'>

Get Shadrul’s stories in your inbox

Join Medium for free to get updates from this writer.

It’s important to note that variables in Python are not explicitly typed. This means that a variable can hold any type of data, such as a number, a string, or a list. For example, the following code assigns a string value to the variable “name”:

name = "John Smith"

And the following code assigns a list value to the variable “numbers”:

numbers = [1, 2, 3, 4, 5]

A variable can also be reassigned to a new value at any time. For example, the following code first assigns the value of 5 to “x”, and then reassigns it the value of 10:

x = 5
x = 10

It’s also possible to perform mathematical operations on variables and store the result in a new variable or the same variable. For example, the following code defines a variable “a” with value 2, and a variable “b” with value 3, then creates a new variable “c” which value is the sum of “a” and “b”:

a = 2
b = 3
c = a + b

In summary, variables are an essential concept in programming and Python provides a simple and flexible way to work with them. They allow you to store, manipulate and retrieve data in your code, and they are essential to control the flow of a program.

--

--