This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
Vector = List[float] | |
def scale(scalar: float, vector: Vector) -> Vector: | |
return [scalar * num for num in vector] | |
# typechecks; a list of floats qualifies as a Vector. | |
new_vector = scale(2.0, [1.0, -4.2, 5.4]) | |
print(new_vector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def greeting(name: str) -> str: | |
return 'Hello ' + name | |
print(greeting("Ventsi")) | |
print(greeting(42)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def capitalize_sentences(sentences): | |
result = sentences.copy() | |
for index, item in enumerate(result): | |
result[index] = item.capitalize() | |
return result | |
sentences = ["this is test sentence", "I love Python", "positive thinking is nice!"] | |
print("Before calling the function:", sentences) | |
capitalized_sentences = capitalize_sentences(sentences) | |
print("After calling the function:", sentences) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_anagrams(word1, word2): | |
""" | |
Check if the two passed words are anagrams. | |
Returns True if the word1 and word2 are anagrams, otherwise returns False | |
""" | |
assert type(word1) == str, "The word1 must be a string" | |
assert type(word2) == str, "The word2 must be a string" | |
return sorted(word1) == sorted(word2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_anagrams(word1, word2): | |
""" | |
Check if the two passed words are anagrams. | |
Returns True if the word1 and word2 are anagrams, otherwise returns False | |
""" | |
assert type(word1) == str | |
assert type(word12) == str | |
return sorted(word1) == sorted(word2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def capitalize_sentences(sentences): | |
for index, item in enumerate(sentences): | |
sentences[index] = item.capitalize() | |
sentences = ["this is test sentence", "I love Python", "positive thinking is nice!"] | |
print("Before calling the function:", sentences) | |
capitalize_sentences(sentences) | |
print("After calling the function:", sentences) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
file = open("filename.txt") | |
file.write("Python is awesome!") | |
except: | |
print("An error occured when writing to the file") | |
finally: | |
file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
print(sorted([1, 2, 3])) | |
except Exception as error: | |
print("Something went wrong") | |
print(error) | |
else: | |
print("Nothing went wrong") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
print(sorted(1)) | |
except Exception as error: | |
print("Something went wrong") | |
print(error) | |
else: | |
print("Nothing went wrong") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
print(int("Hello")) | |
except ValueError as error: | |
# Use your logger to log the error | |
# Example: | |
# logger.error(error) |
NewerOlder