Skip to content
Python Course
def fibanocci(n):
if n == 0 or n == 1:
return 0
result = 0
x, y = 0, 1
for i in range(0, n - 1):
x = y
y = result
result = x + y
return result
a = fibanocci(10)
print (a)
def is_palindrome_sentence(sentence):
sentence_no_punctuation = ""
for char in sentence:
if char.isalnum():
sentence_no_punctuation += char.casefold()
return sentence_no_punctuation[::-1] == sentence_no_punctuation
word = "Do geese see God?"
if is_palindrome_sentence(word):
print("'{}' is a palindrome".format(word))
else:
print("'{}' is not a palindrome".format(word))
num = [5, 4, 3, 2, 1]
print(num[2:-1].pop(1))
Python Basics
my_list = [
(
"some_album",
"Matt Weisman",
2022,
["python", "sql", "power bi"]
),
(
"another_album",
"Holly Nusser",
2023,
["pandas", "postgre", "line"]
)
]
for i, (title, artist, year, songs) in enumerate(my_list):
print(i, title, artist, year, songs)
# To replace or remove items from a string: string.replace(<pattern>, <replacement value>)
frui = "mango, banana, guava, kiwi, and strawberry"
fruit.replace("and ", "")
print(fruits)
Run cancelled
# list.reverse(reverse=True) a list in place alphabetically (keyword argument in optional)
numbers = [num for num in range(1, 11)]
numbers.reverse()
print(numbers)
Run cancelled
# list.sort() to sort a list in place
vegetables = ['broccoli', 'carrot', 'cauliflower', 'eggplant', 'tomato', 'zucchini']
vegetables.sort(reverse=False)
print(vegetables)
Run cancelled
# to generate a new list when sorting use sorted(<list>, reverse=True) function
fruit = fruits = ["mango", "banana", "guava", "kiwi", "strawberry"]
fruit_sorted = sorted(fruit)
print(fruit_sorted)
Run cancelled
# any() returns True if there are any "truthy" values, and False if not
# works with any iterable object
my_list = [0, '', False, None]
result = any(my_list)
print(result) #False since these are all falsy values
my_list = [1, "", False, None]
result = any(my_list)
print(result) #True since 1 is a truthy value
my_list = [0, "a", False, None]
result = any(my_list)
print(result) #True since "a" is a truthy value
my_list = [0, "a", True, None]
result = any(my_list)
print(result) #True since "True" is a truthy value
Run cancelled
# To sum an iterable use the sum() function
my_list = [1, 2, 3, 4]
sum(my_list)
FUNCTIONS
Raising Exceptions
Run cancelled
def banner_text(text):
screen_width = 50
if len(text) > screen_width - 4:
raise ValueError(f"String {text} is larger than specified width {screen_width}")
Run cancelled
# Other errors
# TypeError