Skip to content

Commit c996fa2

Browse files
Finished part 1
1 parent 3611f90 commit c996fa2

Some content is hidden

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

49 files changed

+592
-1
lines changed

Learnpython/Making decisions based on conditions/Conditional statements/What_i_learned_new.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ in practice31.py, i learned about a function name.endwith('a'), which returns bo
22
in practice33.py, i learned about a function name.startwith('x'), which returns boolean value.
33
in practice33.py, i learned about a function len(name), which returns length of the str.
44
in practice34.py, i learned about priority of AND OR NOT, order: NOT > AND > OR.
5+
in practice51.py, i learned about a function isnumeric(), which returns boolean value.
6+
in practice78.py, i learned about function invocation with parameter names, this will help to keep default values untouchable.
7+
in practice83.py, i learned about docstrings to know more visit: https://peps.python.org/pep-0257/
58

Learnpython/Quiz/question1.png

77.4 KB
Loading

Learnpython/Quiz/question1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
count_posters = int(input('How many posters will be printed? '))
2+
3+
def calculate_cost(count_posters):
4+
if count_posters >= 125:
5+
cost = (count_posters // 125) * 50 + count_posters * 1.25
6+
else:
7+
cost = 50 + count_posters * 1.25
8+
print(f"This will cost {cost} USE.")
9+
10+
calculate_cost(count_posters)

Learnpython/Quiz/question2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Ask the user for integer a, b, and c and print the smallest of them.
2+
3+
a = int(input('a=? '))
4+
b = int(input('b=? '))
5+
c = int(input('c=? '))
6+
7+
def smallest(a, b, c):
8+
if b > a < c:
9+
print(a)
10+
elif a > b < c:
11+
print(b)
12+
elif b > c < a:
13+
print(c)
14+
15+
smallest(a, b, c)

Learnpython/Quiz/question3.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Printing average value
2+
3+
count_numbers = int(input('How many numbers do you have? '))
4+
5+
sum = 0
6+
for i in range(0, count_numbers):
7+
number = int(input('Provide a number: '))
8+
sum += number
9+
print(f"The average is {sum/count_numbers}")

Learnpython/Quiz/question4.png

64.3 KB
Loading

Learnpython/Quiz/question4.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
def calculate_rental_cost(day=1, age=30):
3+
if age < 30:
4+
base_fare = 220
5+
else:
6+
base_fare = 100
7+
8+
free_of_charge = day // 7
9+
day = day - free_of_charge
10+
return day * base_fare
11+
12+
print(calculate_rental_cost(1, 17))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Nested for loops – introduction
2+
3+
for i in range (1, 11):
4+
for j in range (1, 11):
5+
print(i, 'x', j, '=', i*j)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Nested for loops – explanation
2+
3+
for i in range(1, 10):
4+
for j in range(1, 2):
5+
print(str(i) + str(i) + str(i) + str(i) + str(i) + str(i) + str(i) + str(i) + str(i))
6+
7+
# Another code
8+
9+
line_to_print = ''
10+
for i in range(1, 10):
11+
for j in range(1, 10):
12+
line_to_print += str(i)
13+
print(line_to_print)
14+
line_to_print = ''
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Nested while loops
2+
3+
# Example
4+
final_score = 0
5+
counter = 0
6+
7+
while counter < 10:
8+
current_number = input('Please provide a number to add: ')
9+
while not current_number.isnumeric():
10+
current_number = input('That\'s not a number! Try again: ')
11+
final_score += int(current_number)
12+
counter += 1
13+
14+
print('The score is', final_score)
15+
16+
# Exercise
17+
18+
n_numbers = int(input("How many numbers will be given? "))
19+
sum = 0
20+
i = 0
21+
22+
while i < n_numbers:
23+
user_input = input("Provide a number: ")
24+
while not user_input.isnumeric():
25+
user_input = input("Wrong! That's not a number! Try again: ")
26+
i += 1
27+
sum += int(user_input)
28+
29+
print(sum/n_numbers)
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Use a loop to calculate and print the value of 10 factorial (10!).
2+
3+
factorial = 1
4+
for i in range(1, 11):
5+
factorial *= i
6+
print(factorial)
7+
8+
# Keep asking the user to guess a secret number until they get it right. The secret number is 8.
9+
answer = input('Guess our secret number! ')
10+
11+
while True:
12+
if int(answer) == 8:
13+
print("Correct!")
14+
break
15+
else:
16+
if int(answer) > 8:
17+
print("Too big!")
18+
else:
19+
print("Too small!")
20+
answer = input("Guess out secret number! ")
21+
22+
23+
# (-) hyphens and (=) signs
24+
25+
line = ''
26+
for i in range(8):
27+
for j in range(12):
28+
if (i+j) % 2 == 0:
29+
line += "-"
30+
else:
31+
line += "="
32+
print(line)
33+
line = ''

Learnpython/Repeating code multiple times/The for loop/practice42.py

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# for loop with range() – explanation
2+
3+
for i in range(2, 16):
4+
print('Current value:', i)
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# for loop with range() – Exercise 1
2+
3+
for i in range(1, 21):
4+
print(i)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# for loop with range() – Exercise 2
2+
3+
current_age = int(input('What is your age? '))
4+
5+
for i in range(2, 6):
6+
print(f"In {i} years, you will be {current_age + i} years old!")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# for loop with strings
2+
3+
# Example
4+
secret_message = 'hello from the other side'
5+
e_count = 0
6+
7+
for letter in secret_message:
8+
if letter == 'e':
9+
e_count = e_count + 1
10+
print(e_count)
11+
12+
# Exercise
13+
user_input = input('Please provide a password: ')
14+
15+
digit_count = 0
16+
for i in user_input:
17+
if i.isnumeric():
18+
digit_count+=1
19+
20+
print(f"Your password contains {digit_count} digits")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# while loop - example
2+
3+
counter = 1
4+
while counter <= 10:
5+
print('Current value:', counter)
6+
counter = counter + 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# while loop - explanation
2+
3+
# Modify the template code so that it only prints even numbers from 2 to 10.
4+
5+
counter = 2
6+
while counter <= 10:
7+
print('Current value:', counter)
8+
counter = counter + 2
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# A convenient shortcut
2+
3+
counter = 1
4+
counter = counter + 1 # is the same as counter += 1
5+
counter = counter - 5 # is the same as counter -= 5
6+
counter = counter * 2 # is the same as counter *= 2
7+
counter = counter / 4 # is the same as counter /= 4
8+
9+
# Exercise
10+
11+
# Write a program that will print all powers of 2, beginning with 21=221=2 and ending with 1024.
12+
13+
target = 2
14+
print("Here are some powers of 2!")
15+
while target <= 1024:
16+
print(target)
17+
target *= 2
18+
19+
print("That's enough!")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Infinite loops
2+
3+
# Example
4+
5+
counter = 1
6+
while counter <= 10:
7+
print('Current value:', counter)
8+
9+
# Exercise
10+
11+
current_value = 1
12+
while current_value < 100:
13+
print(current_value)
14+
current_value += 2
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# while loops for user input
2+
3+
# Example
4+
user_input = input('Please provide a number: ')
5+
while not user_input.isnumeric():
6+
user_input = input('Not a number! Please provide a number: ')
7+
print('Your number is:', user_input)
8+
9+
# Exercise
10+
11+
secret_number = 7
12+
user_input = int(input('Guess the secret number (0-9): '))
13+
while not user_input == secret_number:
14+
user_input = int(input("Wrong! Try again (0-9):" ))
15+
print("Correct!")
Loading
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# break with while loops
2+
3+
# Example
4+
user_input = input('Please provide a number: ')
5+
while True:
6+
if user_input.isnumeric():
7+
break
8+
user_input = input('Not a number! Please provide a number: ')
9+
print('Your number is:', user_input)
10+
11+
12+
# Exercise
13+
year = input('In which year was Python first released? ')
14+
15+
while True:
16+
if int(year) == 1990:
17+
break
18+
year = input("In which year was Python first released? ")
19+
print("Correct!")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# break with for loops
2+
3+
# Example
4+
# The program above asks the user to provide a nickname without digits
5+
user_input = input('Please provide a nickname for yourself. Don\'t use digits! ')
6+
is_valid = True
7+
for character in user_input:
8+
if character.isdigit():
9+
is_valid = False
10+
break
11+
12+
if is_valid == True:
13+
print('Nickname correct!')
14+
else:
15+
print('Nickname incorrect!')
16+
17+
# Exercise
18+
19+
user_number = int(input('Please provide a number from 2 to 1000: '))
20+
21+
flag = True
22+
23+
if 2 <= user_number <= 1000:
24+
for i in range(2, user_number):
25+
if user_number % i == 0:
26+
flag = False
27+
if flag == True:
28+
print(f"{user_number} is prime!")
29+
else:
30+
print(f"{user_number} is not prime!")
31+
else:
32+
print("Incorrect number!")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# continue with while loops
2+
3+
# Example
4+
counter = 0
5+
while counter < 10:
6+
counter += 1
7+
if counter == 3:
8+
continue
9+
print('Current value:', counter)
10+
11+
# Exercise
12+
current_value = 0
13+
while current_value < 100:
14+
current_value += 2
15+
if current_value % 6 == 0:
16+
continue
17+
print(current_value)
18+
print('That\'s enough!')
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# continue with for loops
2+
3+
# Example
4+
for i in range(1, 11):
5+
if i % 2 != 0:
6+
continue
7+
print(i)
8+
9+
# Exercise
10+
11+
for i in range(0, 15):
12+
i += 1
13+
remainder = i % 5
14+
if remainder == 0:
15+
continue
16+
print(f"The remainder of dividing {i} by 5 is {remainder}")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# First function
2+
3+
# Example
4+
def hello_world():
5+
print("Hello, World!")
6+
hello_world()
7+
8+
# Exercise
9+
def show_amazing_skills():
10+
print("I can write my own function!")
11+
show_amazing_skills()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Calling your function
2+
3+
# Example
4+
def hello_world():
5+
print('Hello, World!')
6+
7+
hello_world()
8+
9+
# Exercise
10+
def motivate():
11+
print("You can do this!")
12+
13+
motivate()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Function parameters
2+
3+
# Example
4+
def say_my_name(name):
5+
print("Hello, " + name + "!")
6+
7+
# Exercise
8+
def print_number_to(number):
9+
for i in range(1, number+1):
10+
print(i)
11+
12+
print_number_to(50)

0 commit comments

Comments
 (0)