Python Tutorial For Beginners- Part 3

Python is a powerful language used among companies to build web apps, machine learning models, artificial intelligence applications, web scrapping, analyzing data, and more. Fortunately, Python is easy to learn for beginners, and in this third part of this python tutorial series, you will learn more about this language.

1. If & Else Statements

We’ve discussed how to use the if & else statement inside your python code, but there are more things to deep dive into about these statements.

In the last tutorial, we’ve used only two comparison operators who are “< “and”>” but there are more than that. Let’s see an example:

x = 30
y = 30
if x == y:
    print("x is equal to y")

Output:

x is equal to y

The comparison in the if statement evaluates to true and executes the print functions. This operator “==” is called equal, and it is used to evaluate whether these two values are equal or not. Let’s see another example:

x = 40
y = 30
if x != y:
    print("x is not the same as y")

Output:

x is not the same as y

The comparison again evaluates the true condition and executes the print function. This operator “!=” is called not equal, and it is used to evaluate whether these two value is not equal or not, which is true in the previous example.

There are six operators in Python for comparison, and we’ve seen only four of them. The other two left are “greater than or equal” “>=” and let’s see an example:

x = 40
y = 30
if x >= y:
    print("x is not the same as y")

Output:

x is not the same as y

You will have the same result as the previous code since the x is bigger than y, and this operator will evaluate whether the x value is bigger than or equal to y, and even if they are in the same value, you will get the same result unless the y is greater then x.  The last operator is less than or equal “<=” and let’s see a simple example about it:

x = 40
y = 30
if y <= x:
    print("x is bigger than y")

Output:

x is not the same as y

Since y is less than x, it will execute the print function, and if they are the same value, you will get the same result.

2. Elif Statement

Sometimes you need to use many if statements in your code to verify many conditions. In that case, we will need to use the elif statement, which is the if-else statement in one. Let’s see an example:

x = 500
y = 400
if x == y:
    print("x is equal to y")
elif x > y:
    print("x is bigger than y")
else:
    print("y is bigger than x")

Output:

x is bigger than y

The second statement is executed since the x is bigger than y, and we use the if statement first and the last one is else, and in between, we use the elif statement, and you can use as much as you want.

3. Short If & Else

You can create the if-else statement in just one line of code for simplicity. Let’s see an example:

x = 500
y = 400
print("x is equal to y") if x == y else print("x is bigger than y")

Output:

x is bigger than y

You can understand from the previous code that we told Python to print “x is equal to y” if they are equal. If this condition is not met, print “x is bigger than y”.

4. User Input

You can take the input from the user in Python and store the answer in a variable to use later in your code. Let us see an example:

name = input("Please entre your name: ")
print("Your name is: ", name)

When running this code, it will show you the option to write your name, and when you press enter after you write your name, it will store your answer inside the variable name and print it using the print function.

5. Booleans

Python booleans are data variable types built-in Python, and they represent two values, either True or False. Try this command on your python IDE:

x = True
print(type(x))
y = False
print(type(y))

Output:

<class 'bool'>
<class 'bool'>

When you print the type of the True or False value, it will tell you it is bool meaning boolean. Sometimes, we need to know if an expression is True or False, and when you compare two values in Python, it will give you the answer as a boolean. Let’s see an example:

print(10 == 20)
print(20 < 12)
print(30 > 22)

Output:

False
False
True

You can see that the python print function returns the boolean evaluation of these mathematics comparisons. 

Numbers in python cal also be used as boolean values if they have a value meaning either positive or negative is considered True, but if it is zero, then it is deemed False. Let’s see an example:

x = 0
print(bool(x))
y = 3
print(bool(y))
z = -3
print(bool(z))

Output:

False
True
True

You can see it gives us a False for the first value, which is 0, but the other two values are True since they have a positive.

Conclusion

Thanks for reading! This is the third article in the Python for beginners series where you will learn how to program using the python language, and we will discuss more booleans and other stuff about Python in the next one.

Scroll to Top