SlideShare une entreprise Scribd logo
1  sur  56
Télécharger pour lire hors ligne
Python Data Types
Number, String, List Data Types
Python Data Types
Variables can hold values, and every value has a data-type.
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.
Python Data Types
a = 5
The variable a holds integer value five 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.
NUMERIC DATA TYPE
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.
Numbers
Python supports three types of numeric data.
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.
Numbers
a = 5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
Output
The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True
Number Data Type : Examples
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Float
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Float
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Type Conversion
Output
……………………
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
String Data Type
String
Python string is the collection of the characters surrounded by single quotes, double quotes,
or triple quotes.
Syntax:
str = "Hi Python !"
Here, if we check the type of the variable str using a Python script
print(type(str)) #then it will print a string (str).
In Python, strings are treated as the sequence of characters, which means that Python doesn't
support the character data-type;
instead, a single character written as 'p' is treated as the string of length 1.
String
#Using single quotes
str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)
#Using triple quotes
str3 = '''''Triple quotes are
generally used for represent the
multiline or docstring'''
print(str3)
Strings indexing and splitting
Strings indexing and splitting
the slice operator [] is used to access
the individual characters of the
string.
However, we can use the : (colon)
operator in Python to access the
substring from the given string.
Strings indexing and splitting
We can do the negative slicing in the string; it starts from the rightmost character,
which is indicated as -1.
The second rightmost index indicates -2,
and so on.
Reassigning Strings
Updating the content of the strings is as easy as assigning it to a new string.
The string object doesn't support item assignment i.e.,
A string can only be replaced with new string since its content cannot be
partially replaced.
Strings are immutable in Python.
String Operators
Operator Description
+ It is known as concatenation operator used to join the strings
given either side of the operator.
* It is known as repetition operator. It concatenates the
multiple copies of the same string.
in It is known as membership operator. It returns if a particular
sub-string is present in the specified string.
not in It is also a membership operator and does the exact reverse
of in. It returns true if a particular substring is not present in
the specified string.
String Operators
str = "Hello"
str1 = " world"
print(str*3) # prints HelloHelloHello
print(str+str1) # prints Hello world
print(str[4]) # prints o
print(str[2:4]); # prints ll
print('w' in str) # prints false as w is not present in str
print('wo' not in str1) # prints false as wo is present in str1.
String
The format() method is the most flexible and useful method in formatting strings.
The curly braces {} are used as the placeholder in the string and replaced by the
format() method argument.
# Using Curly braces
print("{} and {} both are the best friend".format("Arun","Abhishek"))
#Positional Argument
print("{1} and {0} best players ".format("Virat","Rohit"))
#Keyword Argument
print("{a},{b},{c}".format(a = "James", b = "Peter", c = "Ricky"))
String Concatenation
a = "Hello"
b = "World"
c = a + b
print(c)
To add a space between them, add a " ":
a = "Hello"
b = "World"
c = a + " " + b
print(c)
LIST
Python List
● A list in Python is used to store the sequence of various types of data.
● Python lists are mutable type its mean we can modify its element after it created.
● A list can be defined as a collection of values or items of different types.
● The items in the list are separated with the comma (,) and enclosed with the square
brackets [].
A list can be defined as below
1. L1 = ["John", 102, "USA"]
2. L2 = [1, 2, 3, 4, 5, 6]
List is a collection which is ordered and changeable.
Allows duplicate members.
List indexing and splitting
List indexing and splitting
Unlike other languages, Python provides the flexibility to use the negative indexing also.
The negative indices are counted from the right.
Updating List values
● Lists are the most versatile data structures in Python since they are
mutable, and their values can be updated by using the slice and
assignment operator.
● Python also provides append() and insert() methods, which can be used to
add values to the list.
Append Items in List
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Insert Items in List
To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
Remove List Items
The remove() method removes the specified item.
Example
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Remove Specific Index Element
The pop() method removes the specified index.
Example
Remove the second item:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
Remove Specific Index Element
If you do not specify the index, the pop() method removes the last item.
Example
Remove the last item:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
Remove Specific Index Element
The del keyword also removes the specified index:
Example
Remove the first item:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
Delete the list
The del keyword can also delete the list completely.
Example
Delete the entire list:
thislist = ["apple", "banana", "cherry"]
del thislist
Clear the List
The clear() method empties the list.
The list still remains, but it has no content.
Example
Clear the list content:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
Loop Through a List
You can loop through the list items by using a for loop:
Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Sort List Alphanumerically
List objects have a sort() method that will sort the list alphanumerically, ascending, by
default:
Example
Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
Sort the list numerically
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
Sort Descending
To sort descending, use the keyword argument reverse = True:
Example
Sort the list descending:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
Sort Descending
Sort the list descending:
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)
Copy a List
We cannot copy a list simply by typing list2 = list1, because: list2 will only be a
reference to list1, and changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().
Example
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
Copy a List
Another way to make a copy is to use the built-in method list().
Example
Make a copy of a list with the list() method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
Join Two Lists
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the + operator.
Example
Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Join Two Lists
Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
LIST OPERATIONS
LIST OPERATIONS
LIST FUNCTIONS
Tuple
● Tuples are used to store multiple items in a single variable.
● A tuple is a collection which is ordered and unchangeable.
● Tuples are written with round brackets.
Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Tuple
● Tuple items are ordered, unchangeable, and allow duplicate values.
● Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
● When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change.
● Tuples are unchangeable, meaning that we cannot change, add or remove
items after the tuple has been created. (Immutable)
● Allow Duplicates : they can have items with the same value:
Tuple
T1 = (101, "Peter", 22)
T2 = ("Apple", "Banana", "Orange")
T3 = 10,20,30,40,50
T4 = (10,)
T5 = ()
Tuple indexing and slicing
The indexing and slicing in the tuple are similar to lists.
The indexing in the tuple starts from 0 and goes to length(tuple) - 1.
The items in the tuple can be accessed by using the index [] operator.
Python also allows us to use the colon operator to access multiple items in the
tuple.
Tuple indexing and slicing
Tuple indexing
Negative Indexing
The tuple element can also access by using negative indexing.
The index of -1 denotes the rightmost element and -2 to the second last item
and so on.
Basic Tuple operations
Basic Tuple operations
Python Tuple inbuilt functions

Contenu connexe

Tendances (20)

Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Operators and Control Statements in Python
Operators and Control Statements in PythonOperators and Control Statements in Python
Operators and Control Statements in Python
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 
Strings
StringsStrings
Strings
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Operators in python
Operators in pythonOperators in python
Operators in python
 

Similaire à Python Data Types (1).pdf

Similaire à Python Data Types (1).pdf (20)

Python data type
Python data typePython data type
Python data type
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Data Handling
Data Handling Data Handling
Data Handling
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
List in Python
List in PythonList in Python
List in Python
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
Datatypes in Python.pdf
Datatypes in Python.pdfDatatypes in Python.pdf
Datatypes in Python.pdf
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Data Type In Python.pptx
Data Type In Python.pptxData Type In Python.pptx
Data Type In Python.pptx
 

Dernier

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 

Dernier (20)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

Python Data Types (1).pdf

  • 1. Python Data Types Number, String, List Data Types
  • 2. Python Data Types Variables can hold values, and every value has a data-type. 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.
  • 3. Python Data Types a = 5 The variable a holds integer value five 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.
  • 4.
  • 6. 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.
  • 7. Numbers Python supports three types of numeric data. 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.
  • 8. Numbers a = 5 print("The type of a", type(a)) b = 40.5 print("The type of b", type(b)) c = 1+3j print("The type of c", type(c)) print(" c is a complex number", isinstance(1+3j,complex)) Output The type of a <class 'int'> The type of b <class 'float'> The type of c <class 'complex'> c is complex number: True
  • 9. Number Data Type : Examples Complex: x = 3+5j y = 5j z = -5j print(type(x)) print(type(y)) print(type(z)) Float x = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z)) Float x = 35e3 y = 12E4 z = -87.7e100 print(type(x)) print(type(y)) print(type(z))
  • 12. String Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. Syntax: str = "Hi Python !" Here, if we check the type of the variable str using a Python script print(type(str)) #then it will print a string (str). In Python, strings are treated as the sequence of characters, which means that Python doesn't support the character data-type; instead, a single character written as 'p' is treated as the string of length 1.
  • 13. String #Using single quotes str1 = 'Hello Python' print(str1) #Using double quotes str2 = "Hello Python" print(str2) #Using triple quotes str3 = '''''Triple quotes are generally used for represent the multiline or docstring''' print(str3)
  • 14. Strings indexing and splitting
  • 15. Strings indexing and splitting the slice operator [] is used to access the individual characters of the string. However, we can use the : (colon) operator in Python to access the substring from the given string.
  • 16. Strings indexing and splitting We can do the negative slicing in the string; it starts from the rightmost character, which is indicated as -1. The second rightmost index indicates -2, and so on.
  • 17. Reassigning Strings Updating the content of the strings is as easy as assigning it to a new string. The string object doesn't support item assignment i.e., A string can only be replaced with new string since its content cannot be partially replaced. Strings are immutable in Python.
  • 18. String Operators Operator Description + It is known as concatenation operator used to join the strings given either side of the operator. * It is known as repetition operator. It concatenates the multiple copies of the same string. in It is known as membership operator. It returns if a particular sub-string is present in the specified string. not in It is also a membership operator and does the exact reverse of in. It returns true if a particular substring is not present in the specified string.
  • 19. String Operators str = "Hello" str1 = " world" print(str*3) # prints HelloHelloHello print(str+str1) # prints Hello world print(str[4]) # prints o print(str[2:4]); # prints ll print('w' in str) # prints false as w is not present in str print('wo' not in str1) # prints false as wo is present in str1.
  • 20. String The format() method is the most flexible and useful method in formatting strings. The curly braces {} are used as the placeholder in the string and replaced by the format() method argument. # Using Curly braces print("{} and {} both are the best friend".format("Arun","Abhishek")) #Positional Argument print("{1} and {0} best players ".format("Virat","Rohit")) #Keyword Argument print("{a},{b},{c}".format(a = "James", b = "Peter", c = "Ricky"))
  • 21. String Concatenation a = "Hello" b = "World" c = a + b print(c) To add a space between them, add a " ": a = "Hello" b = "World" c = a + " " + b print(c)
  • 22. LIST
  • 23. Python List ● A list in Python is used to store the sequence of various types of data. ● Python lists are mutable type its mean we can modify its element after it created. ● A list can be defined as a collection of values or items of different types. ● The items in the list are separated with the comma (,) and enclosed with the square brackets []. A list can be defined as below 1. L1 = ["John", 102, "USA"] 2. L2 = [1, 2, 3, 4, 5, 6] List is a collection which is ordered and changeable. Allows duplicate members.
  • 24. List indexing and splitting
  • 25. List indexing and splitting Unlike other languages, Python provides the flexibility to use the negative indexing also. The negative indices are counted from the right.
  • 26. Updating List values ● Lists are the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator. ● Python also provides append() and insert() methods, which can be used to add values to the list.
  • 27. Append Items in List To add an item to the end of the list, use the append() method: Example Using the append() method to append an item: thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)
  • 28. Insert Items in List To insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index: Example Insert an item as the second position: thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)
  • 29. Remove List Items The remove() method removes the specified item. Example Remove "banana": thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
  • 30. Remove Specific Index Element The pop() method removes the specified index. Example Remove the second item: thislist = ["apple", "banana", "cherry"] thislist.pop(1) print(thislist)
  • 31. Remove Specific Index Element If you do not specify the index, the pop() method removes the last item. Example Remove the last item: thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist)
  • 32. Remove Specific Index Element The del keyword also removes the specified index: Example Remove the first item: thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
  • 33. Delete the list The del keyword can also delete the list completely. Example Delete the entire list: thislist = ["apple", "banana", "cherry"] del thislist
  • 34. Clear the List The clear() method empties the list. The list still remains, but it has no content. Example Clear the list content: thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)
  • 35. Loop Through a List You can loop through the list items by using a for loop: Example Print all items in the list, one by one: thislist = ["apple", "banana", "cherry"] for x in thislist: print(x)
  • 36. Sort List Alphanumerically List objects have a sort() method that will sort the list alphanumerically, ascending, by default: Example Sort the list alphabetically: thislist = ["orange", "mango", "kiwi", "pineapple", "banana"] thislist.sort() print(thislist)
  • 37. Sort the list numerically thislist = [100, 50, 65, 82, 23] thislist.sort() print(thislist)
  • 38. Sort Descending To sort descending, use the keyword argument reverse = True: Example Sort the list descending: thislist = ["orange", "mango", "kiwi", "pineapple", "banana"] thislist.sort(reverse = True) print(thislist)
  • 39. Sort Descending Sort the list descending: thislist = [100, 50, 65, 82, 23] thislist.sort(reverse = True) print(thislist)
  • 40. Copy a List We cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. There are ways to make a copy, one way is to use the built-in List method copy(). Example Make a copy of a list with the copy() method: thislist = ["apple", "banana", "cherry"] mylist = thislist.copy() print(mylist)
  • 41. Copy a List Another way to make a copy is to use the built-in method list(). Example Make a copy of a list with the list() method: thislist = ["apple", "banana", "cherry"] mylist = list(thislist) print(mylist)
  • 42. Join Two Lists There are several ways to join, or concatenate, two or more lists in Python. One of the easiest ways are by using the + operator. Example Join two list: list1 = ["a", "b", "c"] list2 = [1, 2, 3] list3 = list1 + list2 print(list3)
  • 43. Join Two Lists Use the extend() method to add list2 at the end of list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list1.extend(list2) print(list1)
  • 46.
  • 48. Tuple ● Tuples are used to store multiple items in a single variable. ● A tuple is a collection which is ordered and unchangeable. ● Tuples are written with round brackets. Create a Tuple: thistuple = ("apple", "banana", "cherry") print(thistuple)
  • 49. Tuple ● Tuple items are ordered, unchangeable, and allow duplicate values. ● Tuple items are indexed, the first item has index [0], the second item has index [1] etc. ● When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. ● Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. (Immutable) ● Allow Duplicates : they can have items with the same value:
  • 50. Tuple T1 = (101, "Peter", 22) T2 = ("Apple", "Banana", "Orange") T3 = 10,20,30,40,50 T4 = (10,) T5 = ()
  • 51. Tuple indexing and slicing The indexing and slicing in the tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) - 1. The items in the tuple can be accessed by using the index [] operator. Python also allows us to use the colon operator to access multiple items in the tuple.
  • 53. Tuple indexing Negative Indexing The tuple element can also access by using negative indexing. The index of -1 denotes the rightmost element and -2 to the second last item and so on.
  • 56. Python Tuple inbuilt functions