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)
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
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-
- Numbers
- Strings
- List
- Tuple
- 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
Floats are numerical values with decimal points that divide the integer from the fractional parts
x = 25.6
y = 200
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
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
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
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
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
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
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))
Let me know in the comments section