Complete Course Available at: https://github.com/Ebad8931/PythonWorkshop
Introducing Python Lists and Strings. The Presentation further explains how to initialize and access data in lists. String manipulation is covered and finally List Comprehensions are introduced.
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.
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