Variables in Python

Variables in Python programming language

Variables in Python programming language

A variable is a location in the memory that we use to store values and data in the memory with a name that we define in our program. It can be easy to deal with data and change the values of variables later when the program is running.

Programmers use variables in their program because it makes their work easy when they will change code without having to rewrite it again.

How to assign a value to variables in Python?

In python, we assign values to our variables by using this sign (=).

The name of the variable is on the left side while the value is on the right side.

Syntax

#____ProgrammerCodes.com____
var_name = value
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
name = "Sarah"
print(name)
#____ProgrammerCodes.com____

Result

Sarah
How to assign a value to variables in Python

What are types of variables in python?

There are different types of variables such as (Number, String, Tuple, lists, Dictionary).

Numbers:

If we want to store numbers, we have to assign a number type of variable. we name the variable as we want and then we write this sign (=), and on the right side, we write the number without quotes.

Syntax

#____ProgrammerCodes.com____
variable_name = number
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
age = 20
print(age)
#____ProgrammerCodes.com____

Result

20
Numbers in Python

Go visit this article here if you want to know more about numbers in Python.

String:

If we want to store characters or texts in our variable, we will do the same thing that we did when we want to assign number types but in this case, we are going to use quotes.

Syntax

#____ProgrammerCodes.com____
variable_name = "text"
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
city = "London"
print(city)
#____ProgrammerCodes.com____

Result

London
String in python

Go visit this article here if you want to know more about string in Python.

List type:

In Python list, we can store multiple values at the same time and we can access them and be able to deal with them in our program.

Syntax

#____ProgrammerCodes.com____
list_name = [value1, value2, value3, value4]
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
mydata = [ "New York", "London" , "Tokyo" , 2021 , 'Paris', 2022 ]
print(mydata)
#____ProgrammerCodes.com____

Result

['New York', 'London', 'Tokyo', 2021, 'Paris', 2022]

You can store different data types (Strings, Numbers…) or just one data type.

lists in python

Go visit this article here if you want to know more about lists in Python.

Tuple type:

A tuple is the same thing as a list but the difference is list can be changed when the program is running and the tuple can’t.

Syntax

#____ProgrammerCodes.com____
tuple_name = (value1, value2, value3, value4)
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
mytuple = ( "Sarah", 20 ,"James", 18 )
print(mytuple)
#____ProgrammerCodes.com____

Result

('Sarah', 20, 'James', 18)
tuples in python

Dictionary type:

In the dictionary type, you can the same thing by storing values and be able to access and change them later. You can store your values as a key-value way of storing data.

Syntax

#____ProgrammerCodes.com____
dictionary_name = {key1:value1, key2:value2, key3:value3, key4:value4}
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
myInfo = {'Name': "John" , 'Age': 26 , 'City': 'London'}
print(myinfo)
#____ProgrammerCodes.com____

Result

{'Name': 'John', 'City': 'London', 'Age': 26}
dictionary in python

Go visit this article here if you want to know more about Dictionary in Python.

Summary

Variables are everywhere in the programming world. Every project in Python or in other programming languages has one type of variable or most of them. When you combine these variables together with other basic programming concepts, you will make a good project.

Tutorial’s Question

Choose the correct type of variables in this list below:

  • Class
  • Function
  • String

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *