STEM with Python – Part 16: Python Fundamentals Ⅲ (Dictionaries, While and For Loops)

Dictionaries, While and For Loops.

Still the very basics of Python.

Hey, what’s up, guys? Since so many things have happened to me for the past couple of weeks, I couldn’t spend some time updating my coding posts. But, I certainly did keep learning it. My main focus in terms of languages is Java and Kotlin – both are crucial for my coding career. But still, Pythion plays a crucial role of my personal computing life, especially for the Ubuntu Server automation and even Android task automation with an app like QPythion.

Anyways, this is the tried part of Pythin fundamentals, here we go!

Dictionaries:

In Python, a dictionary is a built-in data structure that allows you to store and organize data in key-value pairs. Each key in the dictionary maps to a corresponding value, similar to how words are defined in a physical dictionary.

monthConversions = {
    "Jan" : "January",
    "Feb" : "February",
    "Mar" : "March",
    "Apr" : "April",
    "May" : "May",
    "Jun" : "June",
    "Jul" : "July",
    "Aug" : "August",
    "Sep" : "September",
    "Oct" : "October",
    "Nov" : "November",
    "Dec" : "December",
    1 : "One",
    2 : "Two",
    3 : "Three"
}

print(monthConversions["Nov"])
print(monthConversions.get("Dec"))
print(monthConversions.get("Ex"))
print(monthConversions.get("Ex", "Not a valid key"))
print(monthConversions.get(1))
November
December
None
Not a valid key
One

While loop:

In Python, a while loop is a type of loop that allows you to repeat a block of code as long as a certain condition is true

Sample 1:

i = 1
while i <= 10:
    print(i)
    i += 1
print("Done with loop")
1
2
3
4
5
6
7
8
9
10
Done with loop

Sample 2:

secret_word = "giraffe"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while guess != secret_word and not(out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter your guess: ")
        guess_count += 1
    else:
        out_of_guesses = True
if out_of_guesses == True:
    print("You lose")
else:
    print("You win")
Enter your guess: tiger
Enter your guess: cat
Enter your guess: dog
You lose
Enter your guess: tiger
Enter your guess: lion
Enter your guess: giraffe
You win

For loops:

In Python, a for loop is a type of loop that allows you to iterate over a sequence of values and execute a block of code for each value in the sequence.

Sample 1:

for letter in "Giraffe Academy":
    print(letter)
G
i
r
a
f
f
e
 
A
c
a
d
e
m
y

Sample 2:

friends = ["Kelly", "Sarah", "Max"]
for friend in friends:
    print(friend)
Kelly
Sarah
Max

Sample 3:

for index in range(10):
    print(index)
0
1
2
3
4
5
6
7
8
9

Sample 4:

for index in range(3, 10):
    print(index)
3
4
5
6
7
8
9

Sample 5:

friends = ["Kelly", "Sarah", "Max"]
for index in range(len(friends)):
    print(index)
0
1
2

Sample 6:

friends = ["Kelly", "Sarah", "Max"]
for index in range(len(friends)):
    print(friends[index])
Kelly
Sarah
Max

Leave a Reply