Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Data Types: Understanding Numbers, Sequence Type, Boolean, Set, and Dictionary, Cheat Sheet of Mathematics

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

  • What are the different data types in Python?
  • How do you create and manipulate different data types in Python?
  • How do you check the type of a variable in Python?

Typology: Cheat Sheet

2022/2023

Uploaded on 12/22/2022

Sahil1239
Sahil1239 🇮🇳

4 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Types in Python:
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.5
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.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Numeric data type: Integer, Float, Complex
Sequence Type: Strings, List, Tuple
Numbers or Numeric Data Type:
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.
pf3
pf4
pf5

Partial preview of the text

Download Python Data Types: Understanding Numbers, Sequence Type, Boolean, Set, and Dictionary and more Cheat Sheet Mathematics in PDF only on Docsity!

Data Types in Python:

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.

  1. Numbers
  2. Sequence Type
  3. Boolean
  4. Set
  5. Dictionary

Numeric data type: Integer, Float, Complex

Sequence Type: Strings, List, Tuple

Numbers or Numeric Data Type:

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:

  1. Int - Integer value can be any length such as integers 10, 2, 29, -20, - 150 etc. Python has no restriction on the length of an integer. Its value belongs to int
  2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate upto 15 decimal points.
  3. complex - A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts, respectively. The complex numbers like 2.14j, 2.0 + 2.3j, etc.

Sequence Type

String

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:

  1. Normal: “hello world” 2.Raw: A string prefixed with r or R is called a raw string. A 'raw string' is slightly different from a string where backslash \ , is just taken as a backslash, and not escape sequences to represent newlines, tabs, backspace, and so on.

print (list1[ 3 :])

List slicing

print (list1[ 0 : 2 ])

List Concatenation using + operator

print (list1 + list1)

List repetition using * operator

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]

Tuple

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)

Tuple slicing

print (tup[ 1 :])

print (tup[ 0 : 1 ])

Tuple concatenation using + operator

print (tup + tup)

Tuple repetition using * operator

print (tup * 3 )

Adding value to tup. It will throw an error.

t[ 2 ] = "hi"

Dictionary

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'}

Printing dictionary

print (d)

Accesing value using keys

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}