DATA TYPES AND VARIABLES IN PYTHON
In this tutorial, we will learn about Data Types and Variables in Python, we will learn about how many data types are there in python and what is the difference between data type and a variable. There are different data types in python.
Numbers
Python Numbers include integer, floats and complex number and they are represented as int
, float
and complex
in python class.
These different numbers can be assigned to the variables and to verify the type of any object in Python, use the type()
function:
The difference between integers and float numbers is that a float number is separated by a decimal. So ’10’ is an integer and ‘10.5’ is a float number. Python gives us the freedom to store the integer, floating and complex numbers and also lets us do conversion operations between them.
Integers in Python:
Python can hold signed integers. It can hold a value of any length, the only limitation being the amount of memory available.
Float in Python
Float, or “floating point number” is a number, positive or negative, containing one or more decimals. A float value is only accurate upto 15 decimal places. After that, it rounds the number off.
Complex Numbers in Python
Complex numbers are written with a “j” as the imaginary part.
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
List in Python
A list is a collection of items in a particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family.
You can put anything you want into a list, and the items in your list don’t have to be related in any particular way. Because a list usually contains more than one element, it’s a good idea to make the name of your list plural, such as letters, digits, or names.
vegetables = ['potato', 'tomato', 'capsicum', 'chilli']
print(vegetables)
Tuples in Python
Lists work well for storing sets of items that can change throughout the life of a program. The ability to modify lists is particularly important when you’re working with a list of users on a website or a list of characters in a game. However, sometimes you’ll want to create a list of items that cannot change. Tuples allow you to do just that. Python refers to values that cannot change as immutable, and an immutable list is called a tuple.
It is defined within parentheses () where items are separated by commas.
box = (200, 50)
Strings in Python
A string is simply a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like this:
"This is a string." 'This is also a string.'
Dictionaries in Python
A dictionary in Python is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key. A key’s value can be a number, a string, a list, or even another dictionary. In fact, you can use any object that you can create in Python as a value in a dictionary.
fruits = {'value':tomato,'key':red}
Conversion between Data Types
We can convert between different data types by using different type conversion functions like int(), float(), str() etc.
>>> float(5) 5.0
Conversion from float to int will truncate the value (make it closer to zero).
>>> int(10.6) 10 >>> int(-10.6) -10
Conversion to and from string must contain compatible values.
>>> float('2.5') 2.5 >>> str(25) '25'