chapter2
chapter2
George Boorman
Curriculum Manager, DataCamp
Strings are everywhere!
User profiles
Search engines
1 https://unsplash.com/@steve_j
str methods
# Convert to lowercase
current_top_album = current_top_album.lower()
print(current_top_album)
# Change to upppercase
current_top_album = current_top_album.upper()
print(current_top_album)
Double quotes
String variable my_string = "My string"
""
1 https://docs.python.org/3/library/stdtypes.html#string-methods
George Boorman
Curriculum Manager, DataCamp
Problem
# Variables of product prices
price_one = 10
price_two = 20
price_three = 30
price_four = 15
price_five = 25
price_six = 35
# List of prices
prices = [10, 20, 30, 15, 25, 35]
<class 'list'>
<class 'str'>
Python counts values starting from zero for the first element
10
15
35
35
[20, 30]
[starting_element:last_element + 1]
[20, 25]
a_list[::2] Get every other element from the first index onwards
George Boorman
Curriculum Manager, DataCamp
Products list
prices = [10, 20, 30, 15, 25, 35]
# Product IDs
product_ids = ["AG32", "HT91", "PL65", "OS31", "KB07", "TR48"]
1 https://unsplash.com/@sandym10
order_number
date
value
payment_method
ip_address
location
1 https://unsplash.com/@sharonmccutcheon; https://unsplash.com/@jjying
10
{'AG32': 10, 'HT91': 20, 'PL65': 30, 'OS31': 15, 'KB07': 25, 'TR48': 35}
dict_items([('AG32', 10), ('HT91', 20), ('PL65', 30), ('OS31', 15), ('KB07', 25),
('TR48', 35)])
20
George Boorman
Curriculum Manager, DataCamp
Sets
Contain unique data
Unchangeable*
Can add or remove values, but cannot change them
: = Dictionary
No : = Set
# Convert to a set
attendees_set = set(attendees_list)
set
{'Sam Washington', 'Roger Thompson', 'Alan Jones', 'John Smith', 'Brandon Sharp'}
# Sorting a set
sorted(attendees_set)
['Alan Jones', 'Brandon Sharp', 'John Smith', 'Roger Thompson', 'Sam Washington']
No removing values
No changing values
Ordered
Can subset by index i.e., [0]
1 https://unsplash.com/@towfiqu999999
"London"
Set {1, 2, 3} No No No No