This content originally appeared on Level Up Coding - Medium and was authored by Ritchie Pulikottil
Now that, we have covered the ultimate basics to start off with python programming in Part 1, it’s time for us to move on a little further! Also, I would like to thank each and everyone for the wonderful support, It’s a great motivation when people reach out to shower love, opinions, and suggestions. Anyway, without further adieu let’s get started!
In case you are looking for Part 1: https://levelup.gitconnected.com/python-for-beginners-part-1-e044f603900b, here it is :)
Booleans
Now that you have come this far, you’re definitely ready to meet another type in Python! Yup we have already met Integers, Floats, and Strings, Let’s add Booleans into the mix shall we? Booleans can have two values: True and False. We can create them by comparing values, for instance by using the equal operator ==. Like this:
my_boolean = True
print(my_boolean) #Print’s True
print(2 == 3) #Print’s False
print(“hello” == “hello”) #Print’s True
Be careful not to confuse assignment (one equals sign) with comparison (two equals signs). But don’t worry, we’ll be talking more about this later.
It’s not just the equal operator (==) that creates Booleans though, oh no! You can use any of the comparison operators as well, to compare values and create a Boolean. So what are the other comparison operators? Well, we’ve got: equal to ==, not equal to !=, greater than >, smaller than <, greater or equal to >=, smaller or equal to <=. Comparison operators are also called Relational operators.
Now that might seem like a lot to take in, so let’s look at some examples:
x = 7
print(x != 8) #Print’s True
print(x > 5) #Print’s True
print(x < 2) #Print’s False
print(x >= 7) #Print’s True
print(x <= 7)#Print’s True
If Statements
Okay, so our Boolean comparison has figured out if our statement is true or false, now what? Well, one thing you can do is use if statements to run code based on a certain condition, say if the Boolean evaluates to True, then do something, maybe print.
So basically, with If statements, if the condition evaluates to True, the statements are carried out. Otherwise, they aren’t carried out. An if statement looks like this:
if 1>2:
print(“1 is greater”)
I’m not sure if you can notice, (because Medium doesn't allow me to use tab spaces unnecessarily), the statements within the if statement should be indented, and The colon at the end of the expression in the if statement is important, don’t leave it out.
Nested if statements
Ok, but when is life ever that simple? Sometimes we’ll have to perform more complex checks. But that’s no problem because if statements can be nested, one inside the other. This means that the inner if statement is the statement part of the outer one. This is one way to see whether multiple conditions are satisfied. The indentation is used to define the level of nesting.
Else Statements
The else statement can be used to run some statements when the condition of the if statement is False. As with if statements, the code inside the block needs to be indented.
Elif Statements
Too many if/else statements make your code long and hard to read. Two things we definitely don’t want code to be. The best way to solve this is the elif (short for else if) statement. It’s a shortcut to use when chaining if and else statements, making the code shorter and easier to read.
Boolean Logic
The Boolean and, or, not operators allow checking for multiple conditions in an if statement. Let’s start with the and operator. It is true, if both conditions evaluate to True:
The or operator is True if either (or both) of its conditions are True, and False if both conditions are False
Finally, the not operator works a little differently. not takes just one argument and inverts it. The result of not True is False, and not False goes to True. Like this:
Feeling dizzy? No problem, take your time! Today we’ve had a glance at the basics of conditional statements in Python Programming. Up next, we are gonna look at Loops and Functions, which is probably the end of this Python Beginner’s Series HURRAY! until then take care :)
Level Up Coding
Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.
Coding Interview Questions + Land Your Dev Job | Skilled.dev
Python for Beginners (Part 2) was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Ritchie Pulikottil
Ritchie Pulikottil | Sciencx (2021-06-02T12:09:33+00:00) Python for Beginners (Part 2). Retrieved from https://www.scien.cx/2021/06/02/python-for-beginners-part-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.