SlideShare una empresa de Scribd logo
1 de 34
Descargar para leer sin conexión
Assigning Values to Variables
• It doesn’t need explicit declaration to reserve memory space.
• Declaration happens automatically when we assign values to variable.
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
Multiple Assignment
• Single value for multiple variables
• Multiple values for multiple variables in single assignment line.
a = b = c = 100
a = b = c = 1, “john2”, "John
Standard Data Types
• Five standard datatypes
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
Number
• It store numeric values.
• Number object is created when we assign a value to them.
• To delete variable, use keyword del
var1 = 1
var2 = 10
del var1
Number
• Python support four different numeric types.
1. int – signed integer.
2. long – long integer, also represent octal and hexadecimal.
3. float – floating point real values.
4. complex – complex values.
String
• It represented inside of quotation marks.
• Quotes: single or double.
• Subset of string can be taken by using slice operator [ ] and [ : ] with indexes 0 : -1
• Concatenation operator plus ( + ) sign.
• Replication operator star ( * ) sign.
String
str = 'Hello World!’
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
List - Create list
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# list with mixed datatypes
my_list = [1, "Hello", 3.4]
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]
my_list = ['p','r','o','b','e']
print(my_list[0]) # Output: p
print(my_list[2]) # Output: o
print(my_list[4]) # Output: e
print(my_list[-1]) # Output: e
print(my_list[-5]) # Output: p
n_list = ["Happy", [2,0,1,5]]
print(n_list[0][1]) # Output: a
print(n_list[1][3]) # Output: 5
List – Access list element
my_list = ['p','r','o','g','r','a','m','i','z']
# elements 3rd to 5th
print(my_list[2:5])
# elements beginning to 4th
print(my_list[:-5])
# elements 6th to end
print(my_list[5:])
# elements beginning to end
print(my_list[:])
List – Access list element
List – Change or Add elements
# mistake values
odd = [2, 4, 6, 8]
odd[0] = 1 # change the 1st item
print(odd) # Output: [1, 4, 6, 8]
odd[1:4] = [3, 5, 7]
print(odd) # Output: [1, 3, 5, 7]
odd = [1, 3, 5]
odd.append(7)
print(odd)
# Output: [1, 3, 5, 7]
odd.extend([9, 11, 13])
print(odd)
# Output: [1, 3, 5, 7, 9, 11, 13]
List – Delete elements
my_list = ['p','r','o','b','l','e','m’]
del my_list[2] # delete one item
print(my_list) # Output: ['p', 'r', 'b', 'l', 'e', 'm']
del my_list[1:5] # delete multiple items
print(my_list) # Output: ['p', 'm']
del my_list # delete entire list
print(my_list) # Error: List not defined
List Method – append()
• It is used to add elements to the last position of List.
List = ['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)
['Mathematics', 'chemistry', 1997, 2000, 20544]
List Method – insert()
List = ['Mathematics', 'chemistry', 1997, 2000]
List.insert(2,10087)
print(List)
• It is used to insert element at specific position.
['Mathematics', 'chemistry’, 10087, 1997, 2000]
List Method – extend()
• Add multiple values or another list into the end of the current list
List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
# Add List2 to List1
List1.extend(List2)
print(List1)
#Add List1 to List2 now
List2.extend(List1)
print(List2)
[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]
List Method – sum(), count(), len(), min(), max()
List = [1, 2, 3, 4, 5 , 1]
print(sum(List)) //16
print(List.count(1)) //2
print(len(List)) //6
print(min(List)) //1
print(max(List)) //5
List Method – sort(), reverse()
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
#Reverse flag is set True
List.sort(reverse=True)
print(List)
sorted(List)
print(List)
[5.33, 4.445, 3, 2.5, 2.3, 1.054]
[1.054, 2.3, 2.5, 3, 4.445, 5.33]
List Method – pop(), del(), remove()
pop(): Index is not a necessary parameter, if not mentioned takes the last index.
del() : Element to be deleted is mentioned using list name and index.
remove(): Element to be deleted is mentioned using list name and element.
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())
print(List.pop(0))
del List[0]
print(List)
print(List.remove(3))
2.5
2.3
[4.445, 3, 5.33, 1.054, 2.5]
[4.445, 5.33, 1.054, 2.5]
Tuples
• It is a collection of object much like a list.
• The main different between tuple and list is that tuples are immutable.
• It represent as ( ).
• Values of a tuple are syntactically separated by commas.
• Tuple elements cannot be changes.
Tuples - Create
Tuple1 = () //empty tuple
Tuple2 = ('zooming', 'For’) //tuple with strings
list1 = [1, 2, 4, 5, 6]
Tuple3 = tuple (list1) //tuple with the use of list
Tuple4 = (‘zooming’,) * 3 //tuple with repetition
Tuple5 = (5, 'Welcome', 7, ‘zooming’) //tuple with mixed datatypes
Tuples - Concatenation
• Concatenation of tuple is the process of
joining of two or more Tuples.
• Concatenation is done by the use of ‘+’
operator.
• Concatenation of tuples is done always
from the end of the original tuple.
• Other arithmetic operations do not apply on
Tuples.
# Concatenaton of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = (‘Zooming', 'For', ‘stud')
Tuple3 = Tuple1 + Tuple2
print("nTuples after Concatenaton: ")
print(Tuple3
Tuples after Concatenaton:
(0, 1, 2, 3, ‘Zooming', 'For', ‘stud')
Tuples - Slicing
# with Numbers
Tuple1 = tuple(‘ZOOMING')
# From First element
print(Tuple1[1:])
# Reversing the Tuple
print(Tuple1[::-1])
# Printing elements of a Range
print(Tuple1[2:5])
(‘O’,’O’,’M’,’I’,’N’,’G’)
(‘G’,’N’,’I’,’M’,’O’,’O’,’Z’)
(‘O’,’M’,’I’)
Tuples - Deleting
• Tuples are immutable and hence they
do not allow deletion of a part of it.
• Entire tuple gets deleted by the use of
del() method.
• Note- Printing of Tuple after deletion
results to an Error.
# Deleting a Tuple
Tuple1 = (0, 1, 2, 3, 4)
del Tuple1
print(Tuple1)
NameError: name ‘Tuple1’ is not defined
BUILT-IN FUNCTION DESCRIPTION
all() Returns true if all element are true or if tuple is empty
any() return true if any element of the tuple is true. if tuple is empty, return false
len() Returns length of the tuple or size of the tuple
enumerate() Returns enumerate object of tuple
max() return maximum element of given tuple
min() return minimum element of given tuple
sum() Sums up the numbers in the tuple
sorted() input elements in the tuple and return a new sorted list
tuple() Convert an iterable to a tuple.
Tuple – Built-in-Methods
Dictionary
• It is an unordered collection of data values, used to store data values like a map.
• Dictionary holds key:value pair.
• Key value is provided in the dictionary to make it more optimized.
• Each key-value pair in a Dictionary is separated by a colon :,
• whereas each key is separated by a ‘comma’.
• Key – must be unique and immutable datatype.
• Value – can be repeated with any datatype..
Dictionary - Create
# Creating an empty Dictionary
Dict = {}
# Creating a Dictionary with Integer Keys
Dict = {1: 'Zooming', 2: 'For', 3: 'Zooming’}
# Creating a Dictionary with Mixed keys
Dict = {'Name': 'Zooming', 1: [1, 2, 3, 4]}
# Creating a Dictionary with dict() method
Dict = dict({1: 'Zooming', 2: 'For', 3:'Zooming’})
# Creating a Dictionary with each item as a Pair
Dict = dict([(1, 'Zooming'), (2, 'For')])
Dictionary – Adding element
# Creating an empty Dictionary
Dict = {}
# Adding elements one at a time
Dict[0] = 'Zooming'
Dict[2] = 'For'
Dict[3] = 1
# Adding set of values
# to a single Key
Dict['Value_set'] = 2, 3, 4
# Updating existing Key's Value
Dict[2] = 'Welcome’
# Adding Nested Key value to Dictionary
Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'Zooming’}}
Dictionary – Accessing element
# Creating a Dictionary
Dict = {1: 'Zooming', 'name': 'For', 3: 'Zooming'}
# accessing a element using key
print("Acessing a element using key:")
print(Dict['name'])
# accessing a element using key
print("Acessing a element using key:")
print(Dict[1])
# accessing a element using get()
# method
print("Acessing a element using get:")
print(Dict.get(3))
Dictionary – Removing element
Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks’,
'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'},
'B' : {1 : 'Geeks', 2 : 'Life'}}
print("Initial Dictionary: ")
print(Dict)
# Deleting a Key value
del Dict[6]
# Deleting a Key from Nested Dictionary
del Dict['A'][2]
# Deleting a Key using pop()
Dict.pop(5)
# Deleting entire Dictionary
Dict.clear()
METHODS DESCRIPTION
copy() They copy() method returns a shallow copy of the dictionary.
clear() The clear() method removes all items from the dictionary.
pop() Removes and returns an element from a dictionary having the given key.
popitem() Removes the arbitrary key-value pair from the dictionary and returns it as tuple.
get() It is a conventional method to access a value for a key.
dictionary_name.values() returns a list of all the values available in a given dictionary.
str() Produces a printable string representation of a dictionary.
update() Adds dictionary dict2’s key-values pairs to dict
setdefault() Set dict[key]=default if key is not already in dict
keys() Returns list of dictionary dict’s keys
items() Returns a list of dict’s (key, value) tuple pairs
has_key() Returns true if key in dictionary dict, false otherwise
fromkeys() Create a new dictionary with keys from seq and values set to value.
type() Returns the type of the passed variable.
cmp() Compares elements of both dict.

Más contenido relacionado

La actualidad más candente

Data types in python
Data types in pythonData types in python
Data types in pythonRaginiJain21
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in PythonDevashish Kumar
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionarynitamhaske
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Python Collections
Python CollectionsPython Collections
Python Collectionssachingarg0
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 

La actualidad más candente (20)

Python list
Python listPython list
Python list
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Lists
ListsLists
Lists
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
List in Python
List in PythonList in Python
List in Python
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 

Similar a Python Variable Types, List, Tuple, Dictionary

Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docSrikrishnaVundavalli
 
Python Dictionary
Python DictionaryPython Dictionary
Python DictionarySoba Arjun
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptxNawalKishore38
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
list and control statement.pptx
list and control statement.pptxlist and control statement.pptx
list and control statement.pptxssuser8f0410
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Ohgyun Ahn
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxGaneshRaghu4
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPaweł Dawczak
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎juzihua1102
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎anzhong70
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningTURAGAVIJAYAAKASH
 

Similar a Python Variable Types, List, Tuple, Dictionary (20)

Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.doc
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
list and control statement.pptx
list and control statement.pptxlist and control statement.pptx
list and control statement.pptx
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 
python.pdf
python.pdfpython.pdf
python.pdf
 
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learning
 

Más de Soba Arjun

Java interview questions
Java interview questionsJava interview questions
Java interview questionsSoba Arjun
 
Java modifiers
Java modifiersJava modifiers
Java modifiersSoba Arjun
 
Java variable types
Java variable typesJava variable types
Java variable typesSoba Arjun
 
Java basic datatypes
Java basic datatypesJava basic datatypes
Java basic datatypesSoba Arjun
 
Dbms interview questions
Dbms interview questionsDbms interview questions
Dbms interview questionsSoba Arjun
 
C interview questions
C interview questionsC interview questions
C interview questionsSoba Arjun
 
Technical interview questions
Technical interview questionsTechnical interview questions
Technical interview questionsSoba Arjun
 
Php interview questions with answer
Php interview questions with answerPhp interview questions with answer
Php interview questions with answerSoba Arjun
 
Computer Memory Types - Primary Memory - Secondary Memory
Computer Memory Types - Primary Memory - Secondary MemoryComputer Memory Types - Primary Memory - Secondary Memory
Computer Memory Types - Primary Memory - Secondary MemorySoba Arjun
 
Birds sanctuaries
Birds sanctuariesBirds sanctuaries
Birds sanctuariesSoba Arjun
 
Important operating systems
Important operating systemsImportant operating systems
Important operating systemsSoba Arjun
 
Important branches of science
Important branches of scienceImportant branches of science
Important branches of scienceSoba Arjun
 
Important file extensions
Important file extensionsImportant file extensions
Important file extensionsSoba Arjun
 
Java Abstraction
Java AbstractionJava Abstraction
Java AbstractionSoba Arjun
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java PolymorphismSoba Arjun
 
Java Overriding
Java OverridingJava Overriding
Java OverridingSoba Arjun
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner ClassesSoba Arjun
 
java Exception
java Exceptionjava Exception
java ExceptionSoba Arjun
 
java Inheritance
java Inheritancejava Inheritance
java InheritanceSoba Arjun
 

Más de Soba Arjun (20)

Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Java modifiers
Java modifiersJava modifiers
Java modifiers
 
Java variable types
Java variable typesJava variable types
Java variable types
 
Java basic datatypes
Java basic datatypesJava basic datatypes
Java basic datatypes
 
Dbms interview questions
Dbms interview questionsDbms interview questions
Dbms interview questions
 
C interview questions
C interview questionsC interview questions
C interview questions
 
Technical interview questions
Technical interview questionsTechnical interview questions
Technical interview questions
 
Php interview questions with answer
Php interview questions with answerPhp interview questions with answer
Php interview questions with answer
 
Computer Memory Types - Primary Memory - Secondary Memory
Computer Memory Types - Primary Memory - Secondary MemoryComputer Memory Types - Primary Memory - Secondary Memory
Computer Memory Types - Primary Memory - Secondary Memory
 
Birds sanctuaries
Birds sanctuariesBirds sanctuaries
Birds sanctuaries
 
Important operating systems
Important operating systemsImportant operating systems
Important operating systems
 
Important branches of science
Important branches of scienceImportant branches of science
Important branches of science
 
Important file extensions
Important file extensionsImportant file extensions
Important file extensions
 
Java Abstraction
Java AbstractionJava Abstraction
Java Abstraction
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java Polymorphism
 
Java Overriding
Java OverridingJava Overriding
Java Overriding
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
java Exception
java Exceptionjava Exception
java Exception
 
Java Methods
Java MethodsJava Methods
Java Methods
 
java Inheritance
java Inheritancejava Inheritance
java Inheritance
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 

Último (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

Python Variable Types, List, Tuple, Dictionary

  • 1.
  • 2. Assigning Values to Variables • It doesn’t need explicit declaration to reserve memory space. • Declaration happens automatically when we assign values to variable. counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string print counter print miles print name
  • 3. Multiple Assignment • Single value for multiple variables • Multiple values for multiple variables in single assignment line. a = b = c = 100 a = b = c = 1, “john2”, "John
  • 4. Standard Data Types • Five standard datatypes 1. Numbers 2. String 3. List 4. Tuple 5. Dictionary
  • 5. Number • It store numeric values. • Number object is created when we assign a value to them. • To delete variable, use keyword del var1 = 1 var2 = 10 del var1
  • 6. Number • Python support four different numeric types. 1. int – signed integer. 2. long – long integer, also represent octal and hexadecimal. 3. float – floating point real values. 4. complex – complex values.
  • 7. String • It represented inside of quotation marks. • Quotes: single or double. • Subset of string can be taken by using slice operator [ ] and [ : ] with indexes 0 : -1 • Concatenation operator plus ( + ) sign. • Replication operator star ( * ) sign.
  • 8. String str = 'Hello World!’ print str # Prints complete string print str[0] # Prints first character of the string print str[2:5] # Prints characters starting from 3rd to 5th print str[2:] # Prints string starting from 3rd character print str * 2 # Prints string two times print str + "TEST" # Prints concatenated string
  • 9.
  • 10. List - Create list # empty list my_list = [] # list of integers my_list = [1, 2, 3] # list with mixed datatypes my_list = [1, "Hello", 3.4] # nested list my_list = ["mouse", [8, 4, 6], ['a']]
  • 11. my_list = ['p','r','o','b','e'] print(my_list[0]) # Output: p print(my_list[2]) # Output: o print(my_list[4]) # Output: e print(my_list[-1]) # Output: e print(my_list[-5]) # Output: p n_list = ["Happy", [2,0,1,5]] print(n_list[0][1]) # Output: a print(n_list[1][3]) # Output: 5 List – Access list element
  • 12. my_list = ['p','r','o','g','r','a','m','i','z'] # elements 3rd to 5th print(my_list[2:5]) # elements beginning to 4th print(my_list[:-5]) # elements 6th to end print(my_list[5:]) # elements beginning to end print(my_list[:]) List – Access list element
  • 13. List – Change or Add elements # mistake values odd = [2, 4, 6, 8] odd[0] = 1 # change the 1st item print(odd) # Output: [1, 4, 6, 8] odd[1:4] = [3, 5, 7] print(odd) # Output: [1, 3, 5, 7] odd = [1, 3, 5] odd.append(7) print(odd) # Output: [1, 3, 5, 7] odd.extend([9, 11, 13]) print(odd) # Output: [1, 3, 5, 7, 9, 11, 13]
  • 14. List – Delete elements my_list = ['p','r','o','b','l','e','m’] del my_list[2] # delete one item print(my_list) # Output: ['p', 'r', 'b', 'l', 'e', 'm'] del my_list[1:5] # delete multiple items print(my_list) # Output: ['p', 'm'] del my_list # delete entire list print(my_list) # Error: List not defined
  • 15. List Method – append() • It is used to add elements to the last position of List. List = ['Mathematics', 'chemistry', 1997, 2000] List.append(20544) print(List) ['Mathematics', 'chemistry', 1997, 2000, 20544]
  • 16. List Method – insert() List = ['Mathematics', 'chemistry', 1997, 2000] List.insert(2,10087) print(List) • It is used to insert element at specific position. ['Mathematics', 'chemistry’, 10087, 1997, 2000]
  • 17. List Method – extend() • Add multiple values or another list into the end of the current list List1 = [1, 2, 3] List2 = [2, 3, 4, 5] # Add List2 to List1 List1.extend(List2) print(List1) #Add List1 to List2 now List2.extend(List1) print(List2) [1, 2, 3, 2, 3, 4, 5] [2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]
  • 18. List Method – sum(), count(), len(), min(), max() List = [1, 2, 3, 4, 5 , 1] print(sum(List)) //16 print(List.count(1)) //2 print(len(List)) //6 print(min(List)) //1 print(max(List)) //5
  • 19. List Method – sort(), reverse() List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] #Reverse flag is set True List.sort(reverse=True) print(List) sorted(List) print(List) [5.33, 4.445, 3, 2.5, 2.3, 1.054] [1.054, 2.3, 2.5, 3, 4.445, 5.33]
  • 20. List Method – pop(), del(), remove() pop(): Index is not a necessary parameter, if not mentioned takes the last index. del() : Element to be deleted is mentioned using list name and index. remove(): Element to be deleted is mentioned using list name and element. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(List.pop()) print(List.pop(0)) del List[0] print(List) print(List.remove(3)) 2.5 2.3 [4.445, 3, 5.33, 1.054, 2.5] [4.445, 5.33, 1.054, 2.5]
  • 21.
  • 22. Tuples • It is a collection of object much like a list. • The main different between tuple and list is that tuples are immutable. • It represent as ( ). • Values of a tuple are syntactically separated by commas. • Tuple elements cannot be changes.
  • 23. Tuples - Create Tuple1 = () //empty tuple Tuple2 = ('zooming', 'For’) //tuple with strings list1 = [1, 2, 4, 5, 6] Tuple3 = tuple (list1) //tuple with the use of list Tuple4 = (‘zooming’,) * 3 //tuple with repetition Tuple5 = (5, 'Welcome', 7, ‘zooming’) //tuple with mixed datatypes
  • 24. Tuples - Concatenation • Concatenation of tuple is the process of joining of two or more Tuples. • Concatenation is done by the use of ‘+’ operator. • Concatenation of tuples is done always from the end of the original tuple. • Other arithmetic operations do not apply on Tuples. # Concatenaton of tuples Tuple1 = (0, 1, 2, 3) Tuple2 = (‘Zooming', 'For', ‘stud') Tuple3 = Tuple1 + Tuple2 print("nTuples after Concatenaton: ") print(Tuple3 Tuples after Concatenaton: (0, 1, 2, 3, ‘Zooming', 'For', ‘stud')
  • 25. Tuples - Slicing # with Numbers Tuple1 = tuple(‘ZOOMING') # From First element print(Tuple1[1:]) # Reversing the Tuple print(Tuple1[::-1]) # Printing elements of a Range print(Tuple1[2:5]) (‘O’,’O’,’M’,’I’,’N’,’G’) (‘G’,’N’,’I’,’M’,’O’,’O’,’Z’) (‘O’,’M’,’I’)
  • 26. Tuples - Deleting • Tuples are immutable and hence they do not allow deletion of a part of it. • Entire tuple gets deleted by the use of del() method. • Note- Printing of Tuple after deletion results to an Error. # Deleting a Tuple Tuple1 = (0, 1, 2, 3, 4) del Tuple1 print(Tuple1) NameError: name ‘Tuple1’ is not defined
  • 27. BUILT-IN FUNCTION DESCRIPTION all() Returns true if all element are true or if tuple is empty any() return true if any element of the tuple is true. if tuple is empty, return false len() Returns length of the tuple or size of the tuple enumerate() Returns enumerate object of tuple max() return maximum element of given tuple min() return minimum element of given tuple sum() Sums up the numbers in the tuple sorted() input elements in the tuple and return a new sorted list tuple() Convert an iterable to a tuple. Tuple – Built-in-Methods
  • 28.
  • 29. Dictionary • It is an unordered collection of data values, used to store data values like a map. • Dictionary holds key:value pair. • Key value is provided in the dictionary to make it more optimized. • Each key-value pair in a Dictionary is separated by a colon :, • whereas each key is separated by a ‘comma’. • Key – must be unique and immutable datatype. • Value – can be repeated with any datatype..
  • 30. Dictionary - Create # Creating an empty Dictionary Dict = {} # Creating a Dictionary with Integer Keys Dict = {1: 'Zooming', 2: 'For', 3: 'Zooming’} # Creating a Dictionary with Mixed keys Dict = {'Name': 'Zooming', 1: [1, 2, 3, 4]} # Creating a Dictionary with dict() method Dict = dict({1: 'Zooming', 2: 'For', 3:'Zooming’}) # Creating a Dictionary with each item as a Pair Dict = dict([(1, 'Zooming'), (2, 'For')])
  • 31. Dictionary – Adding element # Creating an empty Dictionary Dict = {} # Adding elements one at a time Dict[0] = 'Zooming' Dict[2] = 'For' Dict[3] = 1 # Adding set of values # to a single Key Dict['Value_set'] = 2, 3, 4 # Updating existing Key's Value Dict[2] = 'Welcome’ # Adding Nested Key value to Dictionary Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'Zooming’}}
  • 32. Dictionary – Accessing element # Creating a Dictionary Dict = {1: 'Zooming', 'name': 'For', 3: 'Zooming'} # accessing a element using key print("Acessing a element using key:") print(Dict['name']) # accessing a element using key print("Acessing a element using key:") print(Dict[1]) # accessing a element using get() # method print("Acessing a element using get:") print(Dict.get(3))
  • 33. Dictionary – Removing element Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks’, 'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'}, 'B' : {1 : 'Geeks', 2 : 'Life'}} print("Initial Dictionary: ") print(Dict) # Deleting a Key value del Dict[6] # Deleting a Key from Nested Dictionary del Dict['A'][2] # Deleting a Key using pop() Dict.pop(5) # Deleting entire Dictionary Dict.clear()
  • 34. METHODS DESCRIPTION copy() They copy() method returns a shallow copy of the dictionary. clear() The clear() method removes all items from the dictionary. pop() Removes and returns an element from a dictionary having the given key. popitem() Removes the arbitrary key-value pair from the dictionary and returns it as tuple. get() It is a conventional method to access a value for a key. dictionary_name.values() returns a list of all the values available in a given dictionary. str() Produces a printable string representation of a dictionary. update() Adds dictionary dict2’s key-values pairs to dict setdefault() Set dict[key]=default if key is not already in dict keys() Returns list of dictionary dict’s keys items() Returns a list of dict’s (key, value) tuple pairs has_key() Returns true if key in dictionary dict, false otherwise fromkeys() Create a new dictionary with keys from seq and values set to value. type() Returns the type of the passed variable. cmp() Compares elements of both dict.