Questions from last time.
Python Basics
Questions
The string type is used to store characters.
Quotes are used with strings as follows:
1) single quotes: 'Hello Python'
2) double quotes: "Hello Python"
3) triple quotes: """Hello Python""" or '''Python Python'''
my_string = "Hello Python"
my_string
my_string = "السلام عليكم"
my_string
Python uses UTF-8 unicode by default.
The built-in function str is used used to identify the type of the string and convert objects to strings.
# 1) identify the type of the value stored in the variable called int_number (we ask here if the type of value is int or not)
print(isinstance(my_string, str))
# 2) convert 10 to string
example = 10
print(type(10))
zz = str(example)
type(zz)
strings are immutable which means that once a string is created, it cannot be changed"what's your name?"
x = """what's your name?
I am 20 Years old.
"""
x
'what\'s your name?'
| P | Y | T | H | O | N |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 |
| -6 | -5 | -4 | -3 | -2 | -1 |
we have positive and negative indexes.
strings are sequences of characters and can be sliced.
Slicing syntax is as follows seq[start, stop, step]
# get index 0 which will be the P letter - positive
word = "PYTHON"
word[0]
# get index 3 which will be the H letter - positive
word = "PYTHON"
word[3]
# get index -1 which will be the N letter - negative
word = "PYTHON"
word[-1]
# slicing with step
word = "PYTHON"
word[0:4:1]
# get all
word = "PYTHON"
word[:]
# get what's after index 2 - note that index 2 is included.
word = "PYTHON"
word[2:]
# get what's before index 2 - note that index 2 is NOT included.
word = "PYTHON"
word[:2]
# reverse the order using negaive step
word = "PYTHON"
word[::-1]
# use negative to get the last 3 letters
word = "PYTHON"
word[-3:]
# using the `slice()` built-in function
word = "PYTHON"
word[slice(0,2,1)]
# Palindrome
#racecar
#radar
our_word = "dood"
# if the given word is equal to the reverse of the given word, print "It is a Palindrome"
if our_word == our_word[::-1]:
print("It is a Palindrome")
# Otherwise, print "Give me another word"
else:
print("Give me another word")
\n¶sentence = "I ate a good lunch.\n I will eat everyday."
print(sentence)
\t¶sentence2 = "I ate a good lunch\t\t\t. I will eat everyday."
print(sentence2)
x = """ I am a string.
I am part of the same string.
me... too!"""
x
capitalize()lower()upper()split()startswith()endswith()format()isdigit()join()join()¶# Example 1
x1 = "Python"
x2 = list(x1)
x2
"".join(x2)
capitalize()¶x1 = "cat".capitalize()
print("Capitalizes the first letter:", x1)
upper()¶x2 = "cat".upper()
print(x2)
format method to replace placeholders with values# we have the following two variables:
name = "John"
subject = "linguistics"
"{name} studies {subject}.".format(name=name, subject=subject)
"{} loves {}.".format("John", "Mary")
# padding with spaces
"{item:>8}".format(item="stew")
# we use f-string
f"{name} studies {subject}."
Lists allow you to store other sequences of objects.
We use square brackets for lists []
# store numbers
list_of_numbers = [1,2,3,4]
list_of_numbers
# store words
list_of_words = ["I", "am", "happy"]
list_of_words
# or both
store_both = ["I", "am", 1, 6]
store_both
# you can add other objects such as None type and boolean
["I", "am", 1, 6, True, False, None, 4.5, [1,2,3]]
list is used used to identify the type of the list and construct any empty list.# 1) declare a variable called `my_list` and store a list of 1,2,3
my_list = [1, 2, 3]
# call the `type` method to find the type of the my_list
type(my_list)
# 2) use `isinstance`
isinstance(my_list, list)
# what is the type of "claire"
type("Claire")
type(["Claire"])
[] or listempty_list = []
empty_list
empty_list.append("John")
empty_list
empty_list.append("Johnny")
empty_list
another_empty_list = list()
another_empty_list
list() can be used to list out other sequences. We will discuss this later, but here is an examplelist("Purdue is amazing")
# A list's ordering matters
[1, 2, 3] == [1, 3, 2]
[1, 2, 3] == [1, 2, 3]
strings# get an item by a postive index
colors = ["red", "green", "blue", "black"]
colors[2]
# get an item by a negative index
colors = ["red", "green", "blue", "black"]
colors[-1]
# get a subsequence by identifying start-index and stop-index
colors = ["red", "green", "blue", "black"]
colors[0:3]
# get a subsequence after 2 index
# (Notice the 2 index will be included)
colors = ["red", "green", "blue", "black"]
colors[2:]
# get a subsequence before 2 index
# (Notice the 2 index will NOT be included)
colors = ["red", "green", "blue", "black"]
colors[:2]
# get a subsequence before -2 index
# (Notice the 2 index will be included)
colors = ["red", "green", "blue", "black"]
colors[-2:]
# get a subsequence before 2 index
# (Notice the 2 index will NOT be included)
colors = ["red", "green", "blue", "black"]
colors[:-2]
# Now if we can use seq[start, stop, step]
# colors = ["red", "green", "blue", "black"]
# colors[0:2:1]
# reverse
colors = ["red", "green", "blue", "black"]
colors[::-1]
append()¶# We have a list of cars
cars = ["Honda", "BMW", "Chevrolet"]
# append Toyota to this list
cars.append("Toyota")
# print the updated list
cars
extend()¶cars = ["Honda", "BMW", "Chevrolet"]
cars2 = ["Toyota", "Ford", "Cadillac"]
cars.extend(cars2)
cars
cars.pop()
cars