Skip to content
← Back to Community
Python Basics Tutorial Part 2!
Profile icon

Python Basics

If you haven't seen Session 1, please check it out. Here is the link:


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:

list = ['here' 'is' 'a' 'list']

So this would be our if statement:

if 'here' in list: print("That is in the list!") else: print("That is not in the list.")

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:

That is in the list!

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:

name = input("What's your name?") print(f"Hello, {name}!")

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'.

Whats your name?Layla Hello, Layla!

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:

answer = input("What is 1 + 1?") if answer is '2': print("That is correct!") else: print("That is incorrect.") ```py This is assigning 'answer' as your input, then checking if that input is 2. If it is, then it prints 'That is correct!', else it prints 'That is incorrect". Output: ```py What is 1 + 1?2 That is correct!

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:

your_list = ‘this’, ’is’, ‘my’, ‘list’ for thing in your_list: print(“This is how you use a for loop with a list.”)

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:

This is how you use a for loop with a list. This is how you use a for loop with a list. This is how you use a for loop with a list. This is how you use a for loop with a list.

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

for i in range(5): print(“for loop”)

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

for loop for loop for loop for loop for loop

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:

cookieLovers = 5

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:

while (cookieLovers > 0): cookieLovers -= 1 print(“There are no more cookie lovers left!”)

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:

There are no more cookie lovers left!

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:

def greet(name): print(f“Hello {name}! How are you?”)

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:

greet(Amy) greet(Leo) greet(Stacy) greet(Dave)


Hello Amy! How are you? Hello Leo! How are you? Hello Stacy! How are you? Hello Dave! How are you?

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!

Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon

I made it a string with "" and it solved itself. I couldn't do it with the example text.

Profile icon

def greet(name):
print(f"Hello{name}! How are you?")


Profile icon

I got a name not found error. What should I do?

Profile icon

Also, in the future, please concatenate your tutorials.

Profile icon
Profile icon

Screen Shot 2020-09-25 at 9.25.10 AM

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

answer = input("What is 1 + 1?") if answer is '2': print("That is correct!") else: print("That is incorrect.")

to like this:

answer = input("What is 1 + 1?") if answer is '2': print("That is correct!") else: print("That is incorrect.")

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



Profile icon

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

Profile icon

cool! np :)

Profile icon

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

Profile icon

@CodeLongAndPros thanks, I will fix that!