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

03 standard Data Types

  • 1.
    Instructor: Ebad ullahQureshi Email: ebadullah.qureshi@rutgers.edu N2E Coding Club Python Workshop ‘Strings’ & [Lists]
  • 2.
    Python workshop byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 byEbad 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 ullahQureshi Email: ebadullah.qureshi@rutgers.edu N2E Coding Club Python Workshop ‘Strings’ & [Lists] Complete