Steemers Coding Class 02 - Learn Coding 10 Minutes a Day For Very Beginners

Yo Steemers, This is Pooria.

This is epidose two of a series of articles for Steemrers to learn coding with only 5-10 minutes of practice a day. If it's the first time you see this topic follow from the beginning.

Previous episodes:

Steemers Coding Class - Episode 01

Coding

Conditions

In this article we want to see how we can add conditions to our program, so it behaves differently based on the variables we get from our users. This is a very important foundation of coding which is used daily by professional developers to control the flow of their programs.

Comparing numbers

We can easily compare numbers with each other the same way we used in mathematics. expressions like 4 > 2 meaning four greater than two or 10 <= 5 meaning ten is less than or equal to five are valid expressions in all the programming languages. We also use == for checking equality like 5 == 5 and != for checking inequality like 5 != 2

When a comparison is correct we call it True like 10 > 0 and when an expression is not correct we call it False like 3 > 5.

Remember

  • You can check strings with equality or inequality like: "Pooria" == "Pooria" or "Pooria" != "Fabien"
  • The two sides of a condition should have the same type (both integers or both strings). E.g, this leads to error: "Pooria" > 5

If

The simplest form to check if a condition is True:

if 5 > 2 :
    print("The condition is True")

As you can see after if 5 > 2 I used a character : and started the next line with some extra spaces. Why I did that?

Think and come up with an answer in your mind before reading the next line

In coding we call those extra spaces indention. It's a way to understand a nesting inside the code. Don't get confused with it, it simply means if the conditon is True then do the indented statements. If not True then skip the indented statements.

In Python indents are really important. Let's understand it better with this example:

if 2 > 5 :
    print("User cannot see this because the condition is False")
    print("We are still inside the if block so this print is also not showing")
print("User can see this because we are outside the if block now :-)")

Write the code above in your editor and run it
Change the condition of if into a True condition like 10 > 1 and see the difference

If else

It's another statement which sometimes is used after if statement. It simply means if the condition of if was False then we want the condition inside else to execute.

In this example I use a variable inside my if and get the input from user as we learned in the previous episode.

age = input("Please enter your age")
if age > 18 :
    print("Congrats, you are an adult!")
else :
    print("You still a Teen, enjoy your life :D")

Run the code above and try it yourself.

Try it I'm waiting. Don't scroll :|

Okay wait... it's not working. When I run it I get this error:

Traceback (most recent call last):
  File "python", line 2, in <module>
TypeError: unorderable types: str() > int()

Any idea?

Well data types are important in coding. If you are asking for user's input Python thinks the user is entering some string (text). It does not check if it's an integer (number) or not. You as the coder only know that it's supposed to be an integer so you should convert it:

age = input("Please enter your age")
age = int(age)
if age > 18 :
    print("Congrats, you are an adult!")
else :
    print("You are still a Teen, enjoy your life :D")

int() only works if your text is actually an integer but in the form of string like int("20") which is equal to 20 but int("Hello") is an error.

Try it yourself

Now it's your turn to write something with this new knowledge. Write a program that ask for user's name. If user's name is "Pooria" the program prints "Ahh that guy is an idiot" but if the name is something else the program would say hello to the name.

Example:
Please enter your name: Pooria
Output: Ahh that guy is an idiot
Please enter your name: John
Output: Hello John!

Keep up with learning and remember that talent is nothing compared to consistency.
Pooria

Image source: https://www.instagram.com/setupinspiration/

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
10 Comments