Python Tutorial For Beginners- Part 2

Python is recognized as one of the best languages to learn nowadays since many companies are using this language to build things such as machine learning models web apps. It is also used in the data science field and more. 

In the last tutorial of this series, we’ve seen things related to beginners, such as variables, comments, mathematics operations, and now we will dive more into other things.

1. Python Dictionary

Python dictionary is one of the four types of data that can hold many values inside one variable. It uses a certain way to store them called key:value pair, which is like giving a name to every value inside that dictionary. Let’s see an example:

car = {"Manufacturer": "Tesla", "Model": "Model X"} #Dictionary
print(car)

Output:

{'Manufacturer': 'Tesla', 'Model': 'Model X'}

You can see that we’ve assigned every value to a key, and you can access the value inside the dictionary this way:

car = {"Manufacturer": "Tesla", "Model": "Model X"}
print(car["Manufacturer"])

The code will show you the “Tesla” value since we’ve asked the dictionary to provide the “Manufacturer” key value. 

It is worth noting that dictionaries allow adding new values, ordered to be added at the end of the dictionary. You can also change any item after creating the dictionary but not duplicate it. Let’s see an example of this:

car = {"Manufacturer": "Tesla", "Model": "Model X", "Manufacturer": "BMW"} #Dictionary
print(car)

Output:

{'Manufacturer': 'BMW', 'Model': 'Model X'}

You can see that it prints the “BMW” value for the “Manufacturer” key, and it shows that you will get the new updated value that is duplicated. You can calculate the number of values inside a dictionary by this simple command:

car = {"Manufacturer": "Tesla", "Model": "Model X"}
print(len(car))

The len() function calculates the number of items in the dictionary and not just dictionaries but many other things such as lists.

2. Python Tuples

Python tuples are like dictionaries and lists since they can store multiple values inside one variable. Let’s see an example:

capitals = ("Berlin", "Washington") #Tuple
print(capitals)

Output:

('Berlin', 'Washington')

You can see that tuples are created using round brackets, and they are ordered, but you can not change the items, add, or remove items once you’ve made one. You can access the elements just like lists and dictionaries:

capitals = ("Berlin", "Washington")
print(capitals[1])

It will print “Washington” since this is the second item in the tuple with the index number 1. You can measure the length of the tuple using the len() function:

capitals = ("Berlin", "Washington")
print(len(capitals))

It will tell you that it has two items inside the tuple. Every tuple can have different data types inside the tuple:

random = ("Berlin", 1945)
print(random)

Output:

('Berlin', 1945)

3. Python Sets

Python sets are also like the three previous ones, which are lists, dictionaries, and tuples, and they can store multiple values in one variable:

random = {"Berlin", "Washington"} #Set
print(random)

Output:

{'Berlin', 'Washington'}

The python sets are unchangeable, meaning you can change the items once you created one, but you can add new items and remove them. 

Also, they are unordered, meaning you can not be sure in which order it will appear when you use them, so you can access the item by indexing as we’ve seen with lists, dictionaries, and tuples.

Another thing about sets is that it doesn’t allow duplicate items meaning you can have the same item more than once inside one set:

random = {"Berlin", "Washington", "Berlin"} 
print(random)

Output:

{'Berlin', 'Washington'}

You can see that the third item didn’t appear when we printed the set because it is already duplicated. Actually, you can measure their length using the len() function:

random = {"Berlin", "Washington"}
print(len(random))

You will get the number 2 after running the code, the number of items inside the “random” set. Also, you can have different data types inside one set:

random = {"Berlin", "Washington", 2022}
print(random)

Output:

{2022, 'Berlin', 'Washington'}

You can see that the output is not in the same order as we created the set. It also prints them without any error since we’ve used many data types inside one variable.

4. If & Else Statement

This if statement you will need in almost all of your python codes is used to evaluate if the either expression is True or False and will execute the code inside the if when the condition is true. Let me show you an example:

x = 300
y = 450
if x < y:
    print("x is less than y")

Output:

x is less than y

After you execute the previous code, the python code will print the statement meaning the condition in the if statement was true since x is less than y in the value. Let’s change the evaluation and see what’s happened:

x = 300
y = 450
if x > y:
    print("x is less than y")

When you run the code, you won’t see anything since the evaluation was false, and the python code will ignore anything inside the if statement and complete running what after the if statement. Let’s fix this and make this python code and add the else statement:

x = 500
y = 450
if x < y:
    print("x is less than y")
else:
    print("y is less than x")

Output:

y is less than x

After running the previous code, it will print execute the code inside the else statement since the evaluation inside the if statement is false. 

The benefits of the else statement are to run the code inside it when the evaluation inside the if is false. After running the code inside the else statement, it will complete running the rest of your python code.

Conclusion

Thanks for reading! This is the second part of the python tutorial, and there are many other things you need to understand about the if & else statements. We will see them in the next part of this python for beginners series.

Scroll to Top