Python Variable Types and Their Uses: Everything You Need to Know

Python is one of the most largely used programming languages today because it helps developers to spend less time writing complicated programs and more time making things work. Python is good for people who are in the learning phase of programming and want to learn the basics. It is very easy to use. You just need to know the basics about variables to get better at programming in Python. In this post, we will tell everything about python, like python variables, python variable types, variables in python examples and python variable exercises.

What is a Variable in Python?

A memory address which can change is called a variable. A memory address which cannot change is called a constant. Variable is the name of the place in memory where information is kept. When a variable is saved, space is set aside in memory for it. It tells the computer what a variable is by using numbers, letters, and the “_” character.

Variables are just places in memory where values can be stored. This means that when you make a variable, you set aside some memory space.

The interpreter decides how much memory to give a variable and what it can be used for based on the type of data it holds. So, you can store integers, decimals, or characters in these variables by giving them different data types.

Python Variable Types

Variables in Python

Since everything in Python is an object, data types are classes, and variables are instances (objects) of these classes.

  • Python Numeric Data type

In Python, numbers are stored in the numeric data type.

Python numbers include integers, floating-point numbers, and numbers with more than one value. 

In Python, they are known as the int, float, and complex classes.

int is a type that holds signed integers of any length.

Float: This holds decimal points that move around, and it’s correct upto 15 decimal places.

Complex – Holds numbers that aren’t simple.

We can use the type() function to see which class a variable or a value fit to.

Example:

num1 = 5

print(num1, ‘is of type’, type(num1))

num2 = 2.0

print(num2, ‘is of type’, type(num2))

num3 = 1+2j

print(num3, ‘is of type’, type(num3))

Output:

5 is of type <class ‘int’>

2.0 is of type <class ‘float’>

(1+2j) is of type <class ‘complex’>

In the example above, we made three variables called num1, num2, and num3 and gave them the values 5, 5.0, and 1+2j, respectively.

Read More: Top 10 Python Frameworks You Need to Know for Web Development

Python List Data Type

A list is an ordered group of similar or different items that are separated by commas and put inside brackets []. One example is

languages = [“Swift”, “Java,” “Python”]

Here, we have created a list of name languages with 3 string values inside it.

Access List Items

We use the index number to get to items on a list (0, 1, 2 …). One example is

languages = [“Swift”, “Java”, “Python”]

# access element at index 0

print(languages[0]) # Swift

# access element at index 2

print(languages[2]) # Python

In the example above, we used the index values to get to items in the list of languages.

Languages [0] – Get the first item from the list of languages, like Swift. – Get the third item from the list of languages, like Python.

  • Python Tuple Data Type

Like a list, a tuple is a set of items in the right order. Only one thing is different: tuples can’t be changed. Once a tuple has been made, it can’t be changed.

To store items in a tuple in Python, we use the parentheses (). One example is

product = (‘Xbox’, 499.98)

In this case, product is a tuple with the string value “Xbox” and the number “499.98”

Access Items in a List

In Python, we use the index number to get to tuple items, just like we do with lists. One 

example is

# create a tuple 

product = (‘Microsoft’, ‘Xbox’, 499.99)

# access element at index 0

print(product[0]) # Microsoft

# access element at index 1

print(product[1]) # Xbox

  • Python Set Data Type

Set is a group of unique things that are not in order. Set is made up of values that are separated by commas and put between braces .

 One example is

# create a set named student_id

student_id = {112,116, 118, 115}

# display student_id elements

print(student_id)

# display type of student_id

print(type(student_id))

output:

{112,115, 116, 118}

<class ‘set’>

Here, we’ve made a set called “student info” that has 4 numbers.

Since sets are not ordered, indexing doesn’t make sense. So, the slicing operator [] doesn’t work.

  • Python Dictionary Data Type

The Python dictionary is a list of things that are in order. It stores things in pairs of keys and values.

In this case, each value has a key that is a unique identifier.

Let’s see an example,

# create a dictionary named capital_city

capital_city = {‘Nepal’: ‘Kathmandu’, ‘Italy’: ‘Rome’, ‘England’: ‘London’}

print.(capital_city)

output – {‘Nepal’: ‘Kathmandu’.’Italy’: ‘Rome’, ‘England’: ‘London’}

In this example, we have created a dictionary named capital_city. Here,

Keys are ‘Nepal’, ‘Italy’, ‘England’

Values are ‘Kathmandu’, ‘Rome’, ‘London’

Variable Name in Python Example

A variable can have a short name, like “x” or “y,” or a longer name, like “age,” “carname,” or “total volume.” The rules for variables in Python:

  • The first character of a variable name should be a letter or an underscore.
  • The first letter of a variable name can’t be a number.
  • You can only use letters, numbers, and underscores (A-Z, 0-9, and _) in a variable name.
  • Case is important when naming variables (age, Age and AGE are three different variables)
  • None of the Python keywords can be used as a variable name.

Example

#Legal variable names:

myvar = “Educationnest”

my_var = “Educationnest”

_my_var = “Educationnest”

myVar = “Educationnest”

MYVAR = “Educationnest”

myvar2 = “Educationnest”

#Illegal variable names:

2myvar = “Educationnest”

my-var = “Educationnest”

my var = “Educationnest”

Conclusion

Python is an interpretive high-level programming language, and there is a lot about python. This blog post must have cleared your doubts regarding different variable types in python with suitable examples.

For more interactive topics, visit educationnest.com right away!

Press ESC to close