Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Basics Tutorial Part 2!

Python Basics

If you haven't seen Session 1, please check it out. Here is the link: https://repl.it/talk/learn/Python-Basics-tutorial-Session-1/50861

If, else and input

Let's start with the if, else statement. This basically says if this is true, do this, else, do this. Let's use a list for this....
Here's our list:

So this would be our if statement:

This is saying if 'here' is in the list, 'list'. Then print 'That is in the list!', else print 'That is not in the list'. So your output would be:

Got it? Good lets move on.
Now an input statement is really useful, one with an f-string, and two with an if, else function.

An f-string is used when you want to print a variable in a sentence, here's an example: print(f"Here is the {variable}.") The variable goes in the {}.

The input statement asks the user for some specific input. Then you store that input in a variable. For example, you want to ask the user's name, the input would be the name the user types.
So this would be your code:

Your assigning 'name' as your input then when the user type their name, the computer stores what they typed in the variable 'name'. Next, your writing a f-string that prints Hello, then the variable 'name'.

You can use if, else with input if you want to check if the user's input is something. So lets say you want to ask them a math question.
This would be your code:

Loops and loops

There are two types of loops, while loops and for loops.

Let's begin with "for loops". You can use "for loops" with a list. This would be your code:

So in this code your telling the computer for every thing (thing is a variable) in ‘your_list’, print “This is how you use a for loop with a list.”
Your Output:

You can also use a for loop to do something a certain amount of times.
Here is what I mean:

Your basically telling the computer to print “for loop” 5 times (i is a variable).
Expected Output:

Now, let’s move onto while loops. You use a while loop when you don’t know how many times you need something to happen. This is a little more complex. So let’s say that Cora has 10 friends, 5 of those friends are cookie lovers. So that’s your variable:

Then lets say her friends start turning into cake lovers, one by one. And we want to print “There are no more cookie lovers left!” (What! No more cookie lovers!), now we don’t know how long it will take till we print that. That’s where a while loop come in.
Here’s the code:

So what this code does is it tells the computer while the variable, cookieLovers, is greater than zero, minus 1 cookie lover.
You might be wondering “Wait! Why wouldn’t “There are no more cookie lovers left just keep reprinting cause of the while loop?”, well notice how cookieLovers -= 1 is indented and print(“There are no more cookie lovers left!”) is not indented, that shows that the print statement is not inside the while loop. This way it will only print “There are no cookie lovers left!” once the while loop is finished, and the while loop will only finish if cookieLovers = 0.

So cookieLovers -= 1 is basically a shorter version of cookieLovers = cookieLovers - 1.

So your Output would be:

Do you get it? Alright let’s move on.

Def Function, a.k.a. Reusable Code

The def function is really useful in many ways. It basically helps you reuse your code. So imagine you wanted to say hello to someone, and you really don’t want to write a print statement every, single, time you need to greet someone.
Your def statement:

This is basically saying whenever you have to greet someone, you can write greet(), with the person’s name in the parentheses. And the computer will do whatever is in the def function, because we used a f-string in the function it will print our greeting.
Example Input:


Pretty useful, right?


That’s it for my Python tutorial, I hope you enjoyed it! If you have any advice or complaints please comment and I will fix it.
Remember that in the end, to write a good code, you’ll need your logic, smart thinking, and common sense!


Also, in the future, please concatenate your tutorials.


nice, but try to put colors in the codeblocks.
instead of this:

to like this:

for that, you just add py to the 3 back quotes



@Bookie0 Thanks, I didn't know that. :)


cool! np :)


A list has to be delimited with square brackets. Otherwise, it’s a tuple.


@CodeLongAndPros thanks, I will fix that!