Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / floatassert.py
Created May 1, 2020 14:10
Floating point errors.
value:float = 0.0
for n in range(10):
value+=0.1
print(f"The current sum is: {value}")
assert value == 1.0, ("The value is not equal to 1.0")
@raeq
raeq / countvowels.py
Created May 1, 2020 11:26
Count Vowels
sentence:str = "The quick brown fox jumped over the lazy dog."
vowels:set = {"a","e","i","o","u",}
i:int = 0
for letter in sentence:
if letter in vowels:
i+=1
print(f"We found {i} vowels in: {sentence}")
students = {"a", "b", "c"}
if bool(students):
print (f"We have {len(students)} students in this class.")
else:
print ("We have no students in this class.")
stock = {}
if stock:
print (f"We have {len(stock)} items in stock.")
else:
choice:bool = bool(input("Do you like anchovies (press ENTER for no)?"))
print(f"You chose: {choice}")
capitals = {"London", "New York", "Kuala-Lumpur", "Den Haag", "Bern", "Peking"}
for city in capitals:
if city.find(' ') > 0:
print(f"This city's name is comprised of more than one word: {city}")
elif city.find('-') > 0:
print(f"This city's name is comprised of more than one word: {city}")
else:
print(f"This city's name is just one word: {city}")
#Let's define a string to begin with.
sentence = "The quick brown fox jumped over the lazy dog."
#let's show the string with all characters in lower case
print(f"Lower case representation:\n{ sentence.lower() }")
#let's show the string with all characters in title case
print(f"Title case representation:\n{ sentence.title() }")
students = {"[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"}
for student in students:
print (student)
print (f"1 and 1: {bool(1 and 1)}")
print (f"0 and 0: {bool(0 and 0)}")
print (f"1 and 0: {bool(1 and 0)}")
print (f"not 0: {bool(not 0)}")
print (f"not 1: {bool(not 1)}")
print (f"1 or 1: {bool(1 or 1)}")
print (f"0 or 0: {bool(0 or 0)}")
print (f"1 or 0: {bool(1 or 0)}")
age = input("Please tell me your age: ")
age = int(age)
if age < 21:
print ("You may not hire a vehichle.")
elif age <25:
print ("We will add a 'young driver fee' to your rental bill.")
elif age < 45:
print ("Do you require a child seat for a small daily fee?")
elif age > 55:
age = input("Please tell me your age: ")
age = int(age)
if age < 21:
print ("No alcohol can be served to you.")
else:
print ("Which drink may I prepare for you?")