[1] Python Made EZ! 🐍
h
Bookie0 (6560)

Hîïíīįì everyone!

Hope y'all are doing great! School is starting real soon, so I hope you have been studying to get ready you are enjoying the last of vacation!

So I made this tutorial on python so that others can try to learn from it and get better! Hopefully, what I say will be comprehensive and easy to read.

Most of it I will write, but sometimes I will include some stuff from other websites which explain better than me. I will put what I've taken in italic, and the sources and helpful links at the bottom.

By the way, this is the first of tutorials in languages I'm making!


I will be covering:

  • Hello World!: History of Python
  • Key Terms
  • Comments
  • print
  • Data Types
  • Variables
    - Printing Variables
    - Naming Variables
    - Changing Variables
  • Concatenation
  • Operators
  • Comparison Operators
  • Conditionals
    - if
    - elif
    - else
  • input
  • A Bit of Lists
  • for Loops
  • while Loops
  • Functions
  • Imports
    - time
    - random
    - math
  • Small Programs and Useful Stuff
  • ANSI Escape Codes
  • Links
  • Goodbye World!: End

Well without any further ado, let's get on with it!


Hello World!: History of Python

Python is a general purpose programming language. It was created by Guido Van Rossum and released in 1991. One of the main features of it is its readability, simple syntax, and few keywords, which makes it great for beginners (with no prior experience of coding) to learn it.

Fun fact: Guido Van Rossum was reading the scripts of Monty Python when he was creating the language; he needed "a name that was short, unique, and slightly mysterious" so he decided to call the language Python.

(Last year we had to make a poem on a important person in Computer Science, so I made one on him: https://docs.google.com/document/d/1yf2T2fFaS3Vwk7zkvN1nPOr8XPXJroL1yHI7z5qhaRc/edit?usp=sharing)


Key Terms

Now before we continue, just a few words you should know:

  • Console: The black part located at the right/bottom of your screen

  • Input: stuff that is taken in by the computer (more on this later)

  • Ouput: the information processed and sent out by the computer (usually in the console)

  • Errors: actually, a good thing! Don't worry if you have an error, just try to learn from it and correct it. That's how you can improve, by knowing how to correct errors.

  • Execute: run a piece of code


Comments

Comments are used for explaining your code, making it more readable, and to prevent execution when testing code.

This is how to comment:

You can also have multiline comments:


print

The print() functions is used for outputting a message (object) onto the console. This is how you use it:

You can also triple quotes for big messages.

Example:

Output:


Data Types

Data types are the classification or categorization of data items.

These are the 4 main data types:

int: (integer) a whole number
12 is an int, so is 902.

str: (string) a sequence of characters
"Hi" is a str, so is "New York City".

float: (float) a decimal
-90 is a float, so is 128.84

bool: (boolean) data type with 2 possible values; True and False
Note that True has a capital T and False has a capital F!


Variables

Variables are used for containing/storing information.

Example:

Printing variables:

To print variables, you simply do print(variableName):

Output:

Naming Variables:

You should try to make variables with a descriptive name. For example, if you have a variable with an age, an appropriate name would be age, not how_old or number_years.

Some rules for naming variables:

  • must start with a letter (not a number)
  • no spaces (use underscores)
  • no keywords (like print, input, or, etc.)

Changing Variables:

You can change variables to other values.

For example:

As you can see, we have changed the variable x from the initial value of 18 to 19.


Concatenation

Let's go back to our first 3 variables:

What if we want to make a sentence like this:
Her name is Lucy, she is 25 years old and she measures 160.5 cm.

Of course, we could just print that whole thing like this:
print("Her name is Lucy, she is 25 years old and she measures 160.5 cm.")

But if we want to do this with variables, we could do it something like this:

Aha! If you ran it, you should have gotten this error:

Basically, it means that you cannot concatenate int to str. But what does concatenate mean?

Concatenate means join/link together, like the concatenation of "sand" and "castle" is "sandcastle"

In the previous code, we want to concatenate the bits of sentences ("Her name is ", ", she is", etc.) as well as the variables (name, age, and height).

Since the computer can only concatenate str together, we simply have to convert those variables into str, like so:

Output:


Operators

A symbol or function denoting an operation

Basically operators can be used in math.

List of operators:

  • + For adding numbers (can also be used for concatenation) | Eg: 12 + 89 = 101
  • - For subtracting numbers | Eg: 65 - 5 = 60
  • * For multiplying numbers | Eg: 12 * 4 = 48
  • / For dividing numbers | Eg: 60 / 5 = 12
  • ** Exponentiation ("to the power of") | Eg: 2**3 = 8
  • // Floor division (divides numbers and takes away everything after the decimal point) | Eg: 100 // 3 = 33
  • % Modulo (divides numbers and returns whats left (remainder)) | Eg: 50 % 30 = 20

These operators can be used for decreasing/increasing variables.

Example:

You can replace the + in += by any other operator that you want:

Also: x += y is just a shorter version of writing x = x + y; both work the same


Comparison Operators

Comparsion operators are for, well, comparing things. They return a Boolean value, True or False. They can be used in conditionals.

List of comparison operators:

  • == equal to | Eg: 7 == 7
  • != not equal to | Eg: 7 != 8
  • > bigger than | Eg: 12 > 8
  • < smaller than | Eg: 7 < 9
  • >= bigger than or equal to | Eg: 19 >= 19
  • <= smaller than or equal to | Eg: 1 <= 4

If we type these into the console, we will get either True or False:


Conditionals

Conditionals are used to verify if an expression is True or False.

if

Example: we want to see if a number is bigger than another one.

How to say in english: "If the number 10 is bigger than the number 5, then etc.

How to say it in Python:

All the code that is indented will be inside that if statement. It will only run if the condition is verified.
You can also use variables in conditionals:

elif

elif is basically like if; it checks if several conditions are True

Example:

This program will output:

Because age = 16.

else

else usually comes after the if/elif. Like the name implies, the code inside it only executes if the previous conditions are False.

Example:

Output:

Because age < 18.


input

The input function is used to prompt the user. It will stop the program until the user types something and presses the return key.
You can assign the input to a variable to store what the user types.

For example:

Output:

By default, the input converts what the user writes into str, but you can specify it like this:

Output:

You can also do float(input("")) to convert it to float.


Now, here is a little program summarizing a bit of what you've learnt so far.

Full program:

Now a detailed version:

Output:

An option:

Another option:

Final option:


A bit of lists

A list is a collection which is ordered and changeable. They are written with square braquets: []

Output:

You can access specific items of the list with the index number. Now here is the kinda tricky part. Indexes start at 0, meaning that the first item of the list has an index of 0, the second item has an index of 1, the third item has an index of 2, etc.

You can also use negative indexing: index -1 means the last item, index -2 means the second to last item, etc.

You can add items in the list using append():

Output:

"pork" will be added at the end of the list.

For removing items in the list, use remove():

Output:

You can also use del to remove items at a specific index:

Output:

There are also many other things you can do with lists, check out this: https://www.w3schools.com/python/python_lists.asp for more info!


for loops

A for loop is used for iterating over a sequence. Basically, it runs a piece of code for a specific number of times.

For example:

Output:

You can also use the for loop to print each item in a list (using the list from above):

Output:


while loops

while loops will run a piece of code as long as the condition is True.

For example:

Ouput:

You can also make while loops go on for infinity, like so (useful for spamming lol):

Output:


Functions

Functions are a group of code that will only execute when it is called.

For example, instead having to type a piece of code several times, you can use a function to put that piece of code inside, and then when you need to use it, you can just call it.

Output:

return and arguments

The return statement is used in function. It ends the function and "returns" the result, i.e. the value of the expression following the return keyword, to the caller. It is not mandatory; you don't have to use it.

You can also have arguments inside a functions. This allows you to change the function values. The arguments are in the parenthesis.

For example:


Imports

time

You can use time in your Python programs.

How to make the program wait:

Output:

You can also do this (more simpler):

random

You can use the random module to randomly pick numbers with randint():

The reason I am precising this is because you can also use randrange():

You can also randomly pick an item from a list with choice():

math

First, you already have some functions already built in Python: min() and max(). They return the smallest and biggest value of ints inside the parenthesis, respectively.

For example:

Now for some more modules:

You can use math.floor() and math.ceil() to round up numbers to the nearest or highest int.

For example:

Explanation (from Andrew Sutherland's course): So math.floor() will round up 2.3 to the nearest lowest int, which in this case is 2. This is because, if you imagine it, the floor is on the bottom, so thats why it will round the number to the nearest lowest int.

Vice-versa for math.ceil(); it will round up 2.3 to the nearest highest int, which in this case is 3. This is because ceil is short for ceiling (programmers like to shorten words), and the ceiling is high.

You can also get pi π:

Output:

Here is the full list of all the things you can do with math: https://www.w3schools.com/python/module_math.asp


Small Programs You Can Use

Countdown Program:

Output:

Simple Calculator

First way using eval()

Or another way, using several conditionals, and you can only do "something" + "something" (but with the operators):

But obviously that is pretty long and full of many if/elif.


Some functions that are useful:

"Press ENTER to continue" Prompt:

Spacing in between lines function:

Slowprint:


ANSI Escape Codes

ANSI escape codes are for controlling text in the console. You can use it to make what is in the output nicer for the user.

For example, you can use \n for a new line:

Output:

This makes it look nice, you can start typing on the little prompt arrows >>>.

You can also use \t for tab:

Output:

\v for vertical tab:

Output:

You can also have colors in python:

Output:

And you can have underline and italic:

Output:


Links: Sources and Good Websites

Sources:

Always good to use a bit of help from here and there!

Good Websites you can use:

Interactive:

Goodbye World!: End

Well, I guess this is the end. I hope y'all have learnt something new/interesting! If you have any questions, please comment and I will try my best to answer them.

Have a super day everyone!

PS: STAY 6 FEET APART!!!
My beautiful ASCII art:

You are viewing a single comment. View All
Bookie0 (6560)

Oh yea, that makes it an orange lol thanks for pointing that out ;)

Happy that this helped you! :D @tankerguy1917