Skip to content

Commit 0ff6a34

Browse files
committed
first commit
1 parent 35bbd4d commit 0ff6a34

File tree

65 files changed

+4271
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4271
-0
lines changed

Extras/mario.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Debugging
2+
3+
4+
def main():
5+
height = int(input("Height: "))
6+
pyramid(height)
7+
8+
9+
def pyramid(height):
10+
for i in range(height):
11+
# print(i, end=" ")
12+
print("#" * (i + 1))
13+
14+
15+
if __name__ == "__main__":
16+
main()

Extras/style.pdf

46.3 KB
Binary file not shown.

Extras/style.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `black style.py` from CLI formatted this file
2+
students = {
3+
"Hermione": "Griffindor",
4+
"Harry": "Griffindor",
5+
"Draco": "Slytherin",
6+
"Ron": "Griffindor",
7+
}
8+
for student in students:
9+
print(student)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Ask user for their name
2+
name = input("Hello, friend. What is your name? ") # input gets a string from stdio
3+
# and stores it in a variable
4+
# Say hello to the user
5+
print("hello, " + name)
6+
print("hello,", name) # multiple arguments to print lead to automatic space addition
7+
8+
9+
""" Named Parameters """
10+
# in official python documentation we find:
11+
# print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
12+
# so it may receive many parameters and some of them are optional and
13+
# others are the so-called named parameters.
14+
# Those named parameters have a default value e.g., the end default value is
15+
# the '\n' so that the print function adds everytime the '\n' at its end!
16+
print("hello, ", end="") # no new line!
17+
print(name)
18+
print("hello,", name, sep="_")
19+
20+
# quotes in print
21+
print('hello, "friend" or...', 'hello, "friend"')
22+
23+
# Special string: format String or f-String
24+
print(f"hello, {name}") # watchout for the f before the string!
25+
26+
27+
#############################################################
28+
# String Methods #
29+
#############################################################
30+
name = " pippo johnny "
31+
32+
name = name.strip() # remove whitespaces from str
33+
name = name.capitalize() # capitalize user's name
34+
print(f"hello, {name}")
35+
name = name.title() # title book capitalization
36+
print(f"hello, {name}")
37+
38+
# function chaining
39+
name = input("What is your name bro?\n").strip().title()
40+
print(f"Hello, {name}")
41+
42+
# Splitting strings:
43+
# split user's name into first and last name
44+
first_name, last_name = name.split(" ")
45+
print(f"Hello, {first_name}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
##############################################################
2+
# Integers #
3+
##############################################################
4+
x = input("What's x? ")
5+
y = input("What's y? ")
6+
z = int(x) + int(y)
7+
8+
print(z)
9+
10+
# Nesting functions... exaggerating a bit
11+
print("")
12+
print(int(input("What's x? ")) + int(input("What's y? ")))
13+
14+
15+
##############################################################
16+
# Floats #
17+
##############################################################
18+
x = float(input("What's x? "))
19+
y = float(input("What's y? "))
20+
21+
print(x + y)
22+
23+
# Rounding the result: round(number[ , ndigits])
24+
print(round(x + y, 0))
25+
26+
z = round(x + y)
27+
print(f"{z}")
28+
print(f"{z:,}") # thousands separator US: 1,000
29+
30+
"""Divisions"""
31+
z = round(x / y, 2) # e.g., x = 2, y = 3 -> z = 0.67
32+
print(f"{(x / y):.2f}") # same result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#############################################################
2+
# Functions #
3+
#############################################################
4+
def main():
5+
name = input("What is your name? ")
6+
hello() # it will use the default value of `to`
7+
hello(name)
8+
9+
x = int(input("What's x? "))
10+
print(f"{x} squared is {square(x)}")
11+
12+
13+
def square(n):
14+
return n**2 # same as: return n * n, or pow(n, 2)
15+
16+
17+
def hello(to="World"):
18+
print(f"Hello, {to}!")
19+
20+
21+
main() # in this way we can call `hello()` even though it
22+
# is defined after `main()`

Lecture 00 - Functions, Variables/lecture0.html

+588
Large diffs are not rendered by default.
59.2 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
def main1():
2+
x = int(input("What's x? "))
3+
y = int(input("What's y? "))
4+
5+
if x < y:
6+
print("x is less than y")
7+
elif x == y:
8+
print("x and y are equal")
9+
else:
10+
print("x is greater than y")
11+
12+
13+
# OR
14+
def main2():
15+
x = int(input("What's x? "))
16+
y = int(input("What's y? "))
17+
18+
if x < y or x > y: # x != y
19+
print("x is not equal to y")
20+
else:
21+
print("x and y are equal")
22+
23+
24+
def grade():
25+
score = int(input("Score: "))
26+
27+
if score >= 90 and score <= 100: # AND
28+
print("Grade: A")
29+
elif 80 <= score < 90: # BETWEEN
30+
print("Grade: B")
31+
elif score >= 70: # simplyfication: not necessary to write >= 70 and <= 79
32+
print("Grade: C")
33+
elif score >= 60: # as above, because the first condition met represents the score
34+
print("Grade: D")
35+
else:
36+
print("Grade: F")
37+
38+
39+
def parody():
40+
x = int(input("What's x? "))
41+
42+
if x % 2 == 0:
43+
print("x is even")
44+
else:
45+
print("x is odd")
46+
47+
48+
# Booleans
49+
def is_even(x):
50+
if x % 2 == 0:
51+
return True # capital T!
52+
else:
53+
return False # capital F!
54+
55+
56+
def is_even_pythonic(x):
57+
return True if x % 2 == 0 else False # like as we would say in English
58+
59+
60+
def is_even_best(y):
61+
return y % 2 == 0 # elegant
62+
63+
64+
def check_even():
65+
x = int(input("What's x? "))
66+
67+
if is_even_best(x):
68+
print("x is even")
69+
else:
70+
print("x is odd")
71+
72+
73+
# Match
74+
def house():
75+
name = input("What's your name? ").capitalize()
76+
77+
match name:
78+
case "Harry" | "Hermione" | "Ron":
79+
print("Griffindor")
80+
case "Draco":
81+
print("Slytherin")
82+
case _: # default
83+
print("Who?")
84+
85+
86+
house()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Home Federal Savings Bank
2+
3+
In [season 7, episode 24](https://en.wikipedia.org/wiki/The_Invitations) of [Seinfeld](https://en.wikipedia.org/wiki/Seinfeld), [Kramer](https://en.wikipedia.org/wiki/Cosmo_Kramer) [visits a bank](https://www.youtube.com/watch?v=IN6cJ_wGmsk) that promises to give `$100` to anyone who isn’t greeted with a “hello.” Kramer is instead greeted with a “hey,” which he insists isn’t a “hello,” and so he asks for `$100`. The bank’s manager proposes a compromise: “You got a greeting that starts with an ‘h,’ how does `$20` sound?” Kramer accepts.
4+
5+
6+
In a file called `bank.py`, implement a program that prompts the user for a greeting. If the greeting starts starts with “hello”, output `$0`. If the greeting starts with an “h” (but not “hello”), output `$20`. Otherwise, output `$100`. Ignore any leading whitespace in the user’s greeting, and treat the user’s greeting case-insensitively.
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def main():
2+
greeting = input("Greeting: ")
3+
print(f"${value(greeting)}")
4+
5+
6+
def value(greeting):
7+
str = greeting.lower()
8+
9+
if str.startswith("hello"):
10+
return 0
11+
elif str.startswith("h"):
12+
return 20
13+
else:
14+
return 100
15+
16+
17+
if __name__ == "__main__":
18+
main()

0 commit comments

Comments
 (0)