Python: Beginner Python 2.0! đ
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
andelse
)
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_bills
isnâ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 and
s 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 or
s 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 if
s 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 if
s.
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 if
s and elif
s donât run, the else
runs.
Bye Bye!
I just wrote 1500 words on this tutorial and my fingers are dying but happy coding!!!
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.
I learned this day three of learning python
this was the second thing i learned in the tutorial I used for python