DEV Community

EniRox
EniRox

Posted on

Python Variables and Data Types - Beginner's Guide

It's no question that Python is one of the most sought out programming languages nowadays.

Developers need to have the option to zero in on the programming and execution rather than investing energy composing complex projects. This combined with its simplicity of learning and readability is the place where Python sticks out.

For any programming language, the central ideas are urgent for successful use. In this article, we will find out with regards to the idea of variable and data types in python.

What Are Variables in Python

Variables are only reserved memory areas to store values, this implies that when a variable is made it apportions a space in memory for it. They are utilized to store data to be referred to and controlled by a programming language. They likewise furnish an approach to naming information with nitty-gritty naming, so our program can be better understood by the reader and ourselves.

A variable in python is created as soon as it is assigned a value, no other commands are needed. Every variable in python is considered as an object, the interpreter determines the type of the variable based on the type of value assigned to it.

Python Variable Naming Rules

These are the rules governing how variables are named in Python:

  • Python variables can only begin with letters (A-Z/a-z) and underscores.

  • Python variables are case sensitive, variableName and variableName are two different things.

  • They can only contain alphanumeric characters and underscores

  • Special characters are not allowed, i.e letters (A-Z/a-z), numbers (0-9), and underscores.

Assigning Values to Variables

As mentioned earlier there is no need for explicit declarations or commands when assigning python variables. The declaration happens as soon as a value is assigned to a variable. The equal sign (=) is used for the assignment.

The operand to the left of the = operator is the name of the variable and the one to the right is the value stored in the variable.

name = "my name"
age = 23
height = 17.5

print(name)
print(age)
print(height)
Enter fullscreen mode Exit fullscreen mode

In the code example above, the values "my name", 23, and 17.5 are assigned to the name, age, and height variables respectively, The output of the above code would be -

my name
23
17.5
Enter fullscreen mode Exit fullscreen mode

Standard Data Types

A data type provides a set of values from which an expression may take its values. An object data type defines the type of operations that can be done on the data, the meaning of the data, and the way that its values are stored. Python supports the following data types-

  1. Numbers
  2. Strings
  3. List
  4. Tuple
  5. Dictionary

Numbers

Numbers data types support the following subtypes as well:

  • Integers
  • Floats
  • Booleans
  • Complex Numbers

Integers are used to represent whole-number values, they can either be positive or negative

x = 25
y = 300
#it will be an integer as long as its value is a whole number
Enter fullscreen mode Exit fullscreen mode

Floats are numerical values with decimal points that divide the integer from the fractional parts

x = 25.6
y = 200
Enter fullscreen mode Exit fullscreen mode

Booleans are used for categorical output, its output is either True or False

value = 5 > 6
#value is the boolean variable
print(value)
#this will give an output of True
Enter fullscreen mode Exit fullscreen mode

Complex Numbers this numerical type holds a complex number. The syntax of a complex number is 2+4j, where the numerical values are the real numbers while the letters are the imaginary parts

y = 4 + 10j
Enter fullscreen mode Exit fullscreen mode

Strings

These are identified as a contiguous set of characters enclosed in quotation marks. One can use either single or double quotes to represent strings. Python strings are immutable which means that they cannot be changed after they are created.

str = "Hello World"
str2 = 'Hello World'

Output

Hello World
Hello World
Enter fullscreen mode Exit fullscreen mode

Lists

These are the most versatile and used compound data types. These contain items separated by commas and enclosed within square brackets ([]). Items in a list can be accessed by using their index. Lists are mutable which means that the items can be changed

months = ["january","february","match"]
print(months[0])
print(months[2])

Output

january
february
Enter fullscreen mode Exit fullscreen mode

Tuples

A Tuple is another sequence data type that has similarities with Lists. A Tuple consists of several values separated by commas. Unlike lists, however, tuples are enclosed within parentheses and are immutable (objects cannot be changed)

example_tuple = ("david", 1, 13.4, "ali")
print(example_tuple)
print(example_tuple[2])

Output

("david", 1, 13.4, "ali")
13.4
Enter fullscreen mode Exit fullscreen mode

Dictionary

A dictionary holds key-value pairs with the keys and values are separated by colons (:). A dictionary is unordered and mutable, they are declared within curly braces with pairs separated by commas. The items in a dictionary are accessed by the keys.

user = {"city":"chicago", "name":"ali"}
print(user)
print(user["city"]

Output

{"city":"chicago", "name":"ali"}
chicago
Enter fullscreen mode Exit fullscreen mode

Summary

In this article on python variables and data types, we learned about Python variables and the different data types.

In the subsequent articles, we will dive deeper into the respective data types and what you can do with them.

Exercise

What does the code do, and what data type does it return

x = 56.5
print(type(x))
Enter fullscreen mode Exit fullscreen mode

Let me know in the comments section

Top comments (1)

Collapse
 
elisemorrison profile image
Anonymous

Variables and data types in Python are very important to the programmer. A variable is a name that is used to represent a value. The variable can be thought of as an object that has all the properties of an object but does not have any methods associated with it. I personally believe that other than the programming languages, students also need to learn about That’s why I have just explored this page to find out the best blockchain courses for beginners in 2022. I would encourage students to do the same.