SlideShare a Scribd company logo
1 of 16
Download to read offline
Instructor: Ebad ullah Qureshi
Email: ebadullah.qureshi@rutgers.edu
N2E Coding Club
Python Workshop
‘Strings’ & [Lists]
Python workshop by Ebad ullah Qureshi
Recap
2
In the previous lesson
• Taking Input in Python
• Processing (Algorithms/Problem Solving)
• Displaying Output in Python
Standard Data types in Python:
1. Numbers – Integers or Floats
2. Strings
3. Lists
4. Tuples
5. Sets
6. Dictionaries
Python workshop by Ebad ullah Qureshi
Strings – Creating and Accessing a letter
3
# creating a string
text = 'N2E Python Programming'
# Accessing a letter in a String
print(text[0])
print(text[5])
print(text[-1])
print(text[-4])
N
y
g
m
Python workshop by Ebad ullah Qureshi
Strings – Indexing
4
• Indexing of a string allows to access a particular value in the String
• In Python, an index be either positive or negative
• The string 'N2E Python Programming’ has length 22 and can be indexed in the
following way:
• Accessing values outside the range of string index results in an error
N 2 E P y t H o n P r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
-22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Python workshop by Ebad ullah Qureshi
Strings – Slicing
5
text = 'N2E Python Programming'
# Accessing letters in a string
print(text[1:5])
print(text[:6])
print(text[8:])
2E P
N2E Py
on Programming
Python workshop by Ebad ullah Qureshi
Strings - Updating
6
text = 'N2E Python Programming'
new_text = 'Hello world!'
# Concatenating: text + new_text with & between them
print(text + ' & ' + new_text)
# Updating Strings: Output Hello Python using text and new_text variables
print(new_text[:6] + text[4:10])
# Replacing Python with Java (text[4:10] = 'Java' results in an error)
print(text.replace('Python', 'Java'))
N2E Python Programming & Hello world!
Hello Python
N2E Java Programming
Python workshop by Ebad ullah Qureshi
Strings – Common Operations
7
# Count the number of letters (spaces included) in the string
print(len(text))
# Change Case
print(text.upper())
print(text.lower())
22
N2E PYTHON PROGRAMMING
n2e python programming
Python workshop by Ebad ullah Qureshi
Strings – Checking Presence (in keyword)
8
print('Py' in text)
print('P' in text)
print('w' in text)
print('t' not in text)
print(not 'x' in text) # warning
print(text.startswith('N2'))
print(text.endswith('g'))
True
True
False
False
True
True
True
Python workshop by Ebad ullah Qureshi
Strings - Formatting
9
print('You are enrolled in {year} year in {major} Major. You have '
'currently taken {no_of_credits} credits and your '
'GPA is {gpa}'.format(year='Sophomore',
major='Computer Engineering',
no_of_credits=15, gpa=3.58))
You are enrolled in Sophomore year in Computer Engineering Major. You have
currently taken 15 credits and your GPA is 3.58
Program to display your Year, major, number of credits taken, and GPA.
Python workshop by Ebad ullah Qureshi
Lists - Creating
10
# creating lists
numbered_list = [2, 4, 6, 8, 9, 0, 3, 5, 6]
text_list = ['apple', 'oranges', 'mangoes', 'x', 'y', 'z']
mixed_list = [45, 'This is the second element', 2019, [56, 34, 'xyz']]
# Displaying Lengths
print(len(numbered_list))
print(len(text_list))
print(len(mixed_list))
print(len(mixed_list[3]))
9
6
4
3
Python workshop by Ebad ullah Qureshi
Lists – Adding new elements
11
print(numbered_list)
numbered_list = numbered_list + [33, 55, 10]
print(numbered_list)
numbered_list.append(89)
print(numbered_list)
numbered_list.insert(4, 46)
print(numbered_list)
[2, 4, 6, 8, 9, 0, 3, 5, 6]
[2, 4, 6, 8, 9, 0, 3, 5, 6, 33, 55, 10]
[2, 4, 6, 8, 9, 0, 3, 5, 6, 33, 55, 10, 89]
[2, 4, 6, 8, 46, 9, 0, 3, 5, 6, 33, 55, 10, 89]
Python workshop by Ebad ullah Qureshi
Lists – Deleting elements from list
12
print(numbered_list)
del(numbered_list[4])
print(numbered_list)
del(numbered_list[9:])
print(numbered_list)
[2, 4, 6, 8, 46, 9, 0, 3, 5, 6, 33, 55, 10, 89]
[2, 4, 6, 8, 9, 0, 3, 5, 6, 33, 55, 10, 89]
[2, 4, 6, 8, 9, 0, 3, 5, 6]
Python workshop by Ebad ullah Qureshi
Lists – Updating
13
print(numbered_list)
numbered_list[4] = 7
numbered_list[5:7] = [0, 2, 4, 5]
print(numbered_list)
[2, 4, 6, 8, 9, 0, 3, 5, 6]
[2, 4, 6, 8, 7, 0, 2, 4, 5, 5, 6]
Python workshop by Ebad ullah Qureshi
Doubling all elements of the List
14
print(numbered_list)
for num in numbered_list:
num = num * 2
print(numbered_list)
for i in range(len(numbered_list)):
numbered_list[i] = numbered_list[i] * 2
print(numbered_list)
[2, 4, 6, 8, 7, 0, 2, 4, 5, 5, 6]
[2, 4, 6, 8, 7, 0, 2, 4, 5, 5, 6]
[4, 8, 12, 16, 14, 0, 4, 8, 10, 10, 12]
Python workshop by Ebad ullah Qureshi
List Comprehensions – Squaring elements
15
print(numbered_list)
squared_numbers = [n**2 for n in numbered_list]
print(squared_numbers)
[4, 8, 12, 16, 14, 0, 4, 8, 10, 10, 12]
[16, 64, 144, 256, 196, 0, 16, 64, 100, 100, 144]
List comprehensions are useful to create lists whose contents obey a simple rule. For example, list
of squared numbers created by squaring all the elements in an already created list
Instructor: Ebad ullah Qureshi
Email: ebadullah.qureshi@rutgers.edu
N2E Coding Club
Python Workshop
‘Strings’ & [Lists]
Complete

More Related Content

What's hot

Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21myrajendra
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongIan Kluft
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
Python week 5 2019 2020 for g10 by eng.osama ghandour
Python week 5 2019 2020 for g10 by eng.osama ghandourPython week 5 2019 2020 for g10 by eng.osama ghandour
Python week 5 2019 2020 for g10 by eng.osama ghandourOsama Ghandour Geris
 
Python Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaEdureka!
 
Python and Machine Learning
Python and Machine LearningPython and Machine Learning
Python and Machine Learningtrygub
 
Python functions
Python functionsPython functions
Python functionsAliyamanasa
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
Python03 course in_mumbai
Python03 course in_mumbaiPython03 course in_mumbai
Python03 course in_mumbaivibrantuser
 
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Edureka!
 
Programming in Python
Programming in Python Programming in Python
Programming in Python Tiji Thomas
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced pythonCharles-Axel Dein
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for BioinformaticsJosé Héctor Gálvez
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smileMartin Melin
 

What's hot (20)

Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::Long
 
Python modules
Python modulesPython modules
Python modules
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
Python week 5 2019 2020 for g10 by eng.osama ghandour
Python week 5 2019 2020 for g10 by eng.osama ghandourPython week 5 2019 2020 for g10 by eng.osama ghandour
Python week 5 2019 2020 for g10 by eng.osama ghandour
 
Python Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | Edureka
 
Python and Machine Learning
Python and Machine LearningPython and Machine Learning
Python and Machine Learning
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python functions
Python functionsPython functions
Python functions
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
Python Loop
Python LoopPython Loop
Python Loop
 
Python03 course in_mumbai
Python03 course in_mumbaiPython03 course in_mumbai
Python03 course in_mumbai
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
 
Python basic
Python basicPython basic
Python basic
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced python
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smile
 
Python ppt
Python pptPython ppt
Python ppt
 

Similar to 03 standard Data Types

Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptxNileshBorkar12
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 
Start from the sample code on the pdf Change struct to clas.pdf
Start from the sample code on the pdf  Change struct to clas.pdfStart from the sample code on the pdf  Change struct to clas.pdf
Start from the sample code on the pdf Change struct to clas.pdfbabitasingh698417
 
Effective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good PracticesEffective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good PracticesNaresha K
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen TatarynovWorkshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen TatarynovFwdays
 
Python for R developers and data scientists
Python for R developers and data scientistsPython for R developers and data scientists
Python for R developers and data scientistsLambda Tree
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptadityavarte
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Ziyauddin Shaik
 

Similar to 03 standard Data Types (20)

Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
Array
ArrayArray
Array
 
Start from the sample code on the pdf Change struct to clas.pdf
Start from the sample code on the pdf  Change struct to clas.pdfStart from the sample code on the pdf  Change struct to clas.pdf
Start from the sample code on the pdf Change struct to clas.pdf
 
Python basics
Python basicsPython basics
Python basics
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Effective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good PracticesEffective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good Practices
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen TatarynovWorkshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
 
Python for R developers and data scientists
Python for R developers and data scientistsPython for R developers and data scientists
Python for R developers and data scientists
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 

Recently uploaded

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

03 standard Data Types

  • 1. Instructor: Ebad ullah Qureshi Email: ebadullah.qureshi@rutgers.edu N2E Coding Club Python Workshop ‘Strings’ & [Lists]
  • 2. Python workshop by Ebad ullah Qureshi Recap 2 In the previous lesson • Taking Input in Python • Processing (Algorithms/Problem Solving) • Displaying Output in Python Standard Data types in Python: 1. Numbers – Integers or Floats 2. Strings 3. Lists 4. Tuples 5. Sets 6. Dictionaries
  • 3. Python workshop by Ebad ullah Qureshi Strings – Creating and Accessing a letter 3 # creating a string text = 'N2E Python Programming' # Accessing a letter in a String print(text[0]) print(text[5]) print(text[-1]) print(text[-4]) N y g m
  • 4. Python workshop by Ebad ullah Qureshi Strings – Indexing 4 • Indexing of a string allows to access a particular value in the String • In Python, an index be either positive or negative • The string 'N2E Python Programming’ has length 22 and can be indexed in the following way: • Accessing values outside the range of string index results in an error N 2 E P y t H o n P r o g r a m m i n g 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
  • 5. Python workshop by Ebad ullah Qureshi Strings – Slicing 5 text = 'N2E Python Programming' # Accessing letters in a string print(text[1:5]) print(text[:6]) print(text[8:]) 2E P N2E Py on Programming
  • 6. Python workshop by Ebad ullah Qureshi Strings - Updating 6 text = 'N2E Python Programming' new_text = 'Hello world!' # Concatenating: text + new_text with & between them print(text + ' & ' + new_text) # Updating Strings: Output Hello Python using text and new_text variables print(new_text[:6] + text[4:10]) # Replacing Python with Java (text[4:10] = 'Java' results in an error) print(text.replace('Python', 'Java')) N2E Python Programming & Hello world! Hello Python N2E Java Programming
  • 7. Python workshop by Ebad ullah Qureshi Strings – Common Operations 7 # Count the number of letters (spaces included) in the string print(len(text)) # Change Case print(text.upper()) print(text.lower()) 22 N2E PYTHON PROGRAMMING n2e python programming
  • 8. Python workshop by Ebad ullah Qureshi Strings – Checking Presence (in keyword) 8 print('Py' in text) print('P' in text) print('w' in text) print('t' not in text) print(not 'x' in text) # warning print(text.startswith('N2')) print(text.endswith('g')) True True False False True True True
  • 9. Python workshop by Ebad ullah Qureshi Strings - Formatting 9 print('You are enrolled in {year} year in {major} Major. You have ' 'currently taken {no_of_credits} credits and your ' 'GPA is {gpa}'.format(year='Sophomore', major='Computer Engineering', no_of_credits=15, gpa=3.58)) You are enrolled in Sophomore year in Computer Engineering Major. You have currently taken 15 credits and your GPA is 3.58 Program to display your Year, major, number of credits taken, and GPA.
  • 10. Python workshop by Ebad ullah Qureshi Lists - Creating 10 # creating lists numbered_list = [2, 4, 6, 8, 9, 0, 3, 5, 6] text_list = ['apple', 'oranges', 'mangoes', 'x', 'y', 'z'] mixed_list = [45, 'This is the second element', 2019, [56, 34, 'xyz']] # Displaying Lengths print(len(numbered_list)) print(len(text_list)) print(len(mixed_list)) print(len(mixed_list[3])) 9 6 4 3
  • 11. Python workshop by Ebad ullah Qureshi Lists – Adding new elements 11 print(numbered_list) numbered_list = numbered_list + [33, 55, 10] print(numbered_list) numbered_list.append(89) print(numbered_list) numbered_list.insert(4, 46) print(numbered_list) [2, 4, 6, 8, 9, 0, 3, 5, 6] [2, 4, 6, 8, 9, 0, 3, 5, 6, 33, 55, 10] [2, 4, 6, 8, 9, 0, 3, 5, 6, 33, 55, 10, 89] [2, 4, 6, 8, 46, 9, 0, 3, 5, 6, 33, 55, 10, 89]
  • 12. Python workshop by Ebad ullah Qureshi Lists – Deleting elements from list 12 print(numbered_list) del(numbered_list[4]) print(numbered_list) del(numbered_list[9:]) print(numbered_list) [2, 4, 6, 8, 46, 9, 0, 3, 5, 6, 33, 55, 10, 89] [2, 4, 6, 8, 9, 0, 3, 5, 6, 33, 55, 10, 89] [2, 4, 6, 8, 9, 0, 3, 5, 6]
  • 13. Python workshop by Ebad ullah Qureshi Lists – Updating 13 print(numbered_list) numbered_list[4] = 7 numbered_list[5:7] = [0, 2, 4, 5] print(numbered_list) [2, 4, 6, 8, 9, 0, 3, 5, 6] [2, 4, 6, 8, 7, 0, 2, 4, 5, 5, 6]
  • 14. Python workshop by Ebad ullah Qureshi Doubling all elements of the List 14 print(numbered_list) for num in numbered_list: num = num * 2 print(numbered_list) for i in range(len(numbered_list)): numbered_list[i] = numbered_list[i] * 2 print(numbered_list) [2, 4, 6, 8, 7, 0, 2, 4, 5, 5, 6] [2, 4, 6, 8, 7, 0, 2, 4, 5, 5, 6] [4, 8, 12, 16, 14, 0, 4, 8, 10, 10, 12]
  • 15. Python workshop by Ebad ullah Qureshi List Comprehensions – Squaring elements 15 print(numbered_list) squared_numbers = [n**2 for n in numbered_list] print(squared_numbers) [4, 8, 12, 16, 14, 0, 4, 8, 10, 10, 12] [16, 64, 144, 256, 196, 0, 16, 64, 100, 100, 144] List comprehensions are useful to create lists whose contents obey a simple rule. For example, list of squared numbers created by squaring all the elements in an already created list
  • 16. Instructor: Ebad ullah Qureshi Email: ebadullah.qureshi@rutgers.edu N2E Coding Club Python Workshop ‘Strings’ & [Lists] Complete