Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
In Python programming language, everything is an object. Thus, data types are treated as classes and variables are an instance or object of these classes. There are various data types in Python to represent the types of value. In this article, we will learn about the different Python Data Types and how they are assigned to variables in the following sequence:
Let’s begin.
Variables are used to hold values for different data types. As Python is a dynamically typed language, you don’t need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type. Python enables us to check the type of the variable used in the program. With the help of the type() function, you can find out the type of the variable passed.
Example:
x = 24 y = 14.7 z = "Welcome to Edureka" print(type(x)); print(type(y)); print(type(z));
<type 'int'> <type 'float'> <type 'str'>
A variable is used to hold different types of values. For example, a person’s name must be stored as a string whereas an employee ID must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. The standard data types in Python include:
Now that you know about the standard python data types, let’s move on and understand each of these in detail.
Number is used to store numeric values. Python creates Number objects when a number is assigned to a variable. There are 4 types of numeric data:
In Python, you can use a lower-case L with long integers. However, it is more convenient to use an upper-case L.
Example:
a = 12 print(a, "is of type", type(a)) b = 5.05 print(b, "is of type", type(b)) c = 1+2j print(c, "is complex number?", isinstance(1+2j,complex))
12 is of type <class 'int'> 5.05 is of type <class 'float'> (1+2j) is complex number? True
A string is defined as a sequence of characters represented in the quotation marks. In python, you can use single, double, or triple quotes to define a string.
String handling in python can be done using various inbuilt functions and operators. In the case of string handling, the operator + is used to concatenate two strings.
Example:
str1 = 'Welcome to Edureka' #string str1 str2 = 'Python Programming' #string str2 print (str1[0:3]) print (str1[4]) print (str1 + str2)
Wel c Welcome to Edureka Python Programming
Lists are similar to arrays in C but it can contain different types of data in Python. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].
You can use slice [:] operators to access the data of the list. The concatenation operator (+) is similar to the one in strings.
Example:
list = [20, "welcome", "edureka", 40] print (list[3:]); print (list); print (list + list);
[40] [20, 'welcome'] [20, "welcome", "edureka", 40] [20, "welcome", "edureka", 40, 20, "welcome", "edureka", 40]
A tuple is similar to lists in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().
A tuple is a read-only data structure and you cannot modify the size and value of the items of a tuple.
Example:
tuple = ("welcome", "edureka", 40) print (tuple[1:]); print (tuple); print (tuple + tuple);
('edureka', 40) ('welcome', 'edureka', 40) ('welcome', 'edureka', 40, 'welcome', 'edureka', 40)
Dictionary is an ordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma and enclosed in the curly braces {}.
Example:
dict = {1:'John', 2:'Rachel', 3:'Nancy', 4:'Daniel'}; print("1st name is "+dict[1]); print (dict.keys()); print (dict.values());
Output:
1st name is John [1, 2, 3, 4] ['John', 'Rachel', 'Nancy', 'Daniel']
These are the standard python data types used for holding different values. With this, we have come to the end of our article.
Now check out the Python Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Python Certification Training will help you gain expertise in Quantitative Analysis, data mining, and the presentation of data to see beyond the numbers by transforming your career into Data Scientist role.
Got a question for us? Please mention it in the comments section of “Python Data Types” and we will get back to you.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 30th November,2024 30th November SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
edureka.co