Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python: Beginner Python 2.0! 🐍
imightbenutella (3)

A little advanced Python

Prerequisites

Here’s what you should know before this tutorial

  • Basic Python
    You can refer to my post about Beginner Python for this.

Table of contents

Here’s what you’ll learn during this tutorial

  • Arrays
  • User Input
  • If Statements (including or, comparison, and, not, elif and else)

Arrays

If you checked out my A Beginner’s Guide To Python tutorial, you’ll probably remember what I said about variables being like buckets - but try to forget that comparison now. I got a new one.

Variables are a lot like drawers. But really tiny drawers, only big enough for one thing. And when you open a drawer (variable) you can see everything (it runs the programme)! But what if I want to store a lot of things in one drawer? I can’t do that, because the drawer is too small. And… I also want the drawer to have sections. There is something in Python for that - arrays. In our real life example, an array would be a chest of drawers! It would contain multiple drawers, multiple variables.

In short, an array is a list of variables. But my example was more fun.
But how do you use these arrays? How do you store these variables? Let’s get into that.

This time, I have included a repl with the name ‘project’. It uses everything you’ll learn here in the form of a Python calculator, so you can get a practical example of how everything is used.

Using Arrays

Arrays have a simple formula, and it is pretty similar to variables. To be honest, I think everything in Python eventually comes down to a variable. They’re the building blocks of the Python universe.
(added this because last time someone asked why my tutorial only explains variables. this, and because I eventually got tired of my laptop glitching and closing everything I had written, so I just made it shorter. Sorry.)

Here’s an example of an array.

array = [“bananas”, “apples”, “oranges”]

Shall we walk through the array formula?

array is just the name of the variable. I could even replace it with fruits, like so:

fruits = [“bananas”, “apples”, “oranges”]

The []s are the chests. They contain the variables.

The strings inside are the variables. An array can contain strings, integers or booleans.

The commas separate the variable. These are super important.

And here’s how arrays are used in project!

calc_array = ["addition(+)", "subtraction(-)", "multiply(*)", "divide(/)"]

And by the way, you can also use arrays for single variables!

calc_rules = ["You'll be asked a number (number a). Then you will be asked what problem type you want to do - and you can use the symbol or name, but I recommend the symbol because with the name it can cause spelling errors. Then, you will be asked number b and voila! An example of how to use it is:"]

User Input

This is probably the part of Python you’re the most curious about. How can I get my Python programme to interact with the user?
It’s a really easy way, actually, and it all starts off with a variable. Here’s an example.

name = input(“What is your name?”)

This prints out “What is your name” to the console, and the user can now write their name on the console!

Note: If you are storing the data for later, like in project, you have to convert your input variable. If I want to add two numbers that the user gave, they’ll be recorded as a string and won’t be added together, so I’ll just convert the variable that input() is inside of.

Let’s walk through the input function!

name is the name of the variable. That’s how you’ll refer to the data your user entered throughout the programme.

input() is the function. What’s inside of it gets displayed in the console and the user gets to add their input.


If statements.

I’m warming up my fingers for this one, guys.

If statements are pretty self-explanatory. They only run if a certain thing is true. For example,

pay_bills = True

if pay_bills:
    print(“You have to pay the bills!”)

Walking through…

pay_bills is what triggers the if statement.

if pay_bills: is what the situation has to be. If pay_bills is True, then the if statement can run.

print(“You have to pay the bills) is the result. If pay_bills is True, it’ll print this message.

Basically, if statements are divided into three sections: the trigger, the situation and the result.


Using Not

But what if I want something to happen if pay_billsisn’t true? For that we use the not function.

pay_bills = True

if pay_bills:
    print(“You have to pay the bills!”)
if not pay_bills:
   print(“You do not need to pay the bills!”)

Walking through…

not tells the programme to send this message if pay_bills isn’t True. If it isn’t True, then it’s False.


Using And

Let’s add the variable bills_overdue and add a command in the if statement that also tells the user that their bills are overdue.

pay_bills = True
bills_overdue = True

if pay_bills:
    print(“You have to pay the bills!”)
if not pay_bills:
   print(“You do not need to pay the bills!”)
if pay_bills and bills_overdue:
    print(“Your bills are overdue! And new bills are here!)

Walking through faster…

and tells the computer that this command will happen if the two triggers are true.

You can add as many ands in an if as you like.


Using Or

What if I want the computer to do something if one of the triggers is true, and not all of them? For that, we use the or command. This time, I’m only going to edit the first if.

pay_bills = True
bills_overdue = True

if pay_bills or bills_overdue:
    print(“You have to pay the bills!”)
if not pay_bills:
   print(“You do not need to pay the bills!”)
if pay_bills and bills_overdue:
    print(“Your bills are overdue! And new bills are here!)

Jogging through…

or tells the computer to run the result if one of the triggers is true.

You can add as many ors to an if statement as you like.


Comparing

Let’s add the variable bills_price and make the computer decide whether it is too much or no. We can use ==, >=, <= or != for that.

pay_bills = True
bills_overdue = True
bills_price = 10

if pay_bills or bills_overdue:
    print(“You have to pay the bills!”)
if not pay_bills:
   print(“You do not need to pay the bills!”)
if pay_bills and bills_overdue:
    print(“Your bills are overdue! And new bills are here!)
if bills_price == 5:
   print(“you are a good spender!”)
if bills_price >= 5:
   print(“you spend too much!”)
if bills_price <= 5:
    print(“you spend too less!”)
if bills_price != 5:
   print(“try to spend better next time!”)

Jogging through slowly…

if bills_price == 5: tells the computer to run the if if bills_price is equal to 5

if bills_price >= 5: tells the computer to run the if if bills_price is more than 5

if bills_price <= 5: tells the computer to run the if if bills_price is less than 5

if bills_price != 5: tells the computer to run the if if bills_price is not equal to 5


elif

Okay but… what if I want to make all these ifs connected to each other?

pay_bills = True
bills_overdue = True
bills_price = 10

if pay_bills or bills_overdue:
    print(“You have to pay the bills!”)
elif not pay_bills:
   print(“You do not need to pay the bills!”)
elif pay_bills and bills_overdue:
    print(“Your bills are overdue! And new bills are here!)
elif bills_price == 5:
   print(“you are a good spender!”)
elif bills_price >= 5:
   print(“you spend too much!”)
elif bills_price <= 5:
    print(“you spend too less!”)
elif bills_price != 5:
   print(“try to spend better next time!”)

elif tells the computer to run something if the trigger is not the one said above.

You use an elif with multiple ifs.


Else

What if I want a final if statement?

pay_bills = True
bills_overdue = True
bills_price = 10

if pay_bills or bills_overdue:
    print(“You have to pay the bills!”)
elif not pay_bills:
   print(“You do not need to pay the bills!”)
elif pay_bills and bills_overdue:
    print(“Your bills are overdue! And new bills are here!)
elif bills_price == 5:
   print(“you are a good spender!”)
elif bills_price >= 5:
   print(“you spend too much!”)
elif bills_price <= 5:
    print(“you spend too less!”)
elif bills_price != 5:
   print(“try to spend better next time!”)
else:
    print(“we have no information on your bills”)

else does not need a trigger because it runs if all the ifs and elifs don’t run, the else runs.

Bye Bye!

I just wrote 1500 words on this tutorial and my fingers are dying but happy coding!!!

Comments
hotnewtop
IMayBeMe (535)

First things first, this is far from what I would call advanced when it comes to python. Advanced topics would include bitwise operators and the inner workings of Cpython. Secondly, all you do is list syntax, but not explain any of it rendering this as glorified documentation. For example if you wanted to explain arrays/lists, you can talk about how index works and how each element in an array is a group of bits which come together to form a structure in the memory of the program.

MrVoo (84)

Just a correction, they're called lists, not arrays. Also I would still call this beginner stuff.

ch1ck3n (2335)

this was the second thing i learned in the tutorial I used for python

[deleted]

I learned this day three of learning python

[deleted]

@poetaetoes and no i didnt study 5 hours a day