



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of the various data types in Python, including Numbers (Integer, Float, Complex), Sequence Type (Strings, List, Tuple), Boolean, Set, and Dictionary. It explains how to check the type of a variable, create and manipulate different data types, and provides examples for each type.
What you will learn
Typology: Cheat Sheet
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type. a = 53 The variable a holds integer value and we did not define its type. Python interpreter will automatically interpret variables a as an integer type. Python enables us to check the type of the variable used in the program. Python provides us the type() function, which returns the type of the variable passed. a= 50 b="Hello Python" c = 50. print (type(a)) print (type(b)) print (type(c)) Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.
Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. Python provides the type() function to know the data-type of the variable. Similarly, the isinstance() function is used to check an object belongs to a particular class.
Python creates Number objects when a number is assigned to a variable. For example; a = 52 print ("The type of a", type(a)) b = 4. print ("The type of b", type(b)) c = 10 +31j print ("The type of c", type(c)) print (" c is a complex number", isinstance( 10 +31j,complex)) Numeric data types:
The string can be defined as the sequence or collection of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string. Types of strings in Python:
print (list1[ 3 :])
print (list1[ 0 : 2 ])
print (list1 + list1)
print (list1 * 3 ) Output: [1, 'hi', 'Python', 2] [2] [1, 'hi'] [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2] [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
A tuple is similar to the list 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 as we can't modify the size and value of the items of a tuple. Let's see a simple example of the tuple. tup = ("hi", "Python", 2 ) #Printing the tuple print (tup)
print (tup[ 1 :])
print (tup[ 0 : 1 ])
print (tup + tup)
print (tup * 3 )
t[ 2 ] = "hi"
Dictionary is an unordered 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. Values in a dictionary can be of any data type and can be duplicated whereas keys cannot be repeated and must be immutable. Keys are case sensitive if two keys have same name but different cases, it will be treated distinctly. The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}. Consider the following example. d = { 1 :'Jimmy', 2 :'Alex', 3 :'john', 4 :'mike'}
print (d)
print ("1st name is "+d[ 1 ]) print ("2nd name is "+ d[ 4 ]) print(d.get(3)) print (d.keys()) print (d.values()) Output:
set2.add( 10 ) print (set2) #Removing element from the set set2.remove( 2 ) print (set2) Output: {3, 'Python', 'James', 2} {'Python', 'James', 3, 2, 10} {'Python', 'James', 3, 10}