Steemers Coding Class 02 - Learn Coding 10 Minutes a Day For Very BeginnerssteemCreated with Sketch.

in #howto8 years ago

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/

Sort:  

I can't wait for the rest! Your a great teacher!

Thank you for reading! Keep up practicing :-)

This is an excellent series. You have explained in such simple terms that anyone who has an interest will understand. I am saving all your tutorials and will study them later in the weekend, and practice.

Thank you for this.

Thank you, yes I felt there is a need for this type of content all over the internet. Hope it works as expected.

This post has been ranked within the top 80 most undervalued posts in the first half of Nov 15. We estimate that this post is undervalued by $3.22 as compared to a scenario in which every voter had an equal say.

See the full rankings and details in The Daily Tribune: Nov 15 - Part I. You can also read about some of our methodology, data analysis and technical details in our initial post.

If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.

. Great post, thanks for share. Follow and upvoted. cheers

Thanks my friend I appreciate it. The third episode is also out keep grinding.

@p0o thanks, I need to check this one out first before I go to the 3rd :)

At least you got an awesome plan for the weekend!

@p0o yes - that's the idea
thanks to you :)

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.030
BTC 68494.93
ETH 3762.21
USDT 1.00
SBD 3.65