Learn to Code via Tutorials on Repl.it!

← Back to all posts
A simple Python tutorial for |beginners| - [Don't look at this if you are NOT a beginner]
multicoder2021 (1)

A simple Python tutorial for beginners

Hello everyone and this is my first tutorial on repl.it!

[Not my first tutorial ever because I have my own website about HTML!]

Contents:

• The print() function
• The input() function
• The if else and elif loop
• The for loop
• The while loop
• Python operators
• A calculator

The print() function:

The print() function is used to output text to the console.
Here is a very simple example:
Input:

print("Hello World!")

Output:
Hello World!
You can see it outputs 'Hello World!'
You can put double quotes [" "] or single quotes [' '].
Either way, they will show the same thing.
Input:
print("Hello World!")
print('Hello World!')
Output:
Hello World!
Hello World!

The input() function

The input() function takes input from the console and saves it in the browser. You can also 'name' the input, which is actually what you HAVE to do. You can also specify if it is a integer (a number) [int] or string (a letter, word or paragraph) [str].
Here is an example of a variable username, and username being outputted with a string:
Input:

username = input("What is your username?_")
print("Hello" , username , "!")

Output:

What is your name?_multicoder2021
Hello multicoder2021 !

Notice any new stuff in these two lines of code?
Yes! The ','!
The comma basically adds a variable onto the end of the string, but with a space .

The if , else and elif loop

The if, else and elif loop specifies some conditions, and the code under them will be run if those specific conditions are met.
Here is an example:

x = 1
if x == 1:
> print("Ok")
elif x == 2:
> print("Fine")
else:
> print("Really?!?!")

Output:
Ok
You see, it prints 'ok because we CLEARLY state that x has the exact same value and is equal to 1.
But if we took input, then it would print the other things maybe:...

x = int(input("Enter 1, 2 or anything larger:__"))
#Maximum: Infinite instances
if x == 1:
> print("Ok")
elif x == 2:
> print("Fine")
else:
> print("Really?!?!")

Output[Instance1]:
Enter 1,2 or anything larger:__1
Ok
Output[Instance2]:
Enter 1,2 or anything larger:__2
Fine
Output[Instance3]:
Enter 1,2 or anything larger:__3
Really?!?!
__Output[Instance4(and above)]:

Enter 1,2 or anything larger:__31415926535897932384626433832795028841971693993705028944974```

Really?!?!
Yes, yes there are ∞ [infinite] instances.
If you enter a string, it will give this error:
ValueError: invalid literal for int() with base 10: '[stringname]'

The for loop

The for loop can do something many times, without you having to type so many lines of code.
For example, if you wanted to print all the numbers from 1 to 10, you would have to print("1"), print("2"), print("3") and etc.
The amazing, incredible for loop can shorten these 10 priceless, precious lines, into 3 outSTANDing lines!
Here is how:
Input[10 boring lines]:

print("1")
print("2")
print("3")
print("4")
print("5")
print("6")
print("7")
print("8")
print("9")
print("10")

Input[3 outSTANDing lines]:

for i in range(10):
> i = i + 1
> print(i)

Output[The same for both of them]:
1
2
3
4
5
6
7
8
9
10
See?

The while loop

The while loop is just a simple loop.
Or maybe... you can call it an 😈infinite😈 loop.
So basically, you state a variable and its integer OR string value...
and then you just say while this variable's value is this, you should do this.
So that wasn't really that basic so here's an even more basic EXAMPLE:
Input:

x = 1
while x == 1:
 print("X is 1?")

Output:
X is 1?
X is 1?
X is 1?
X is 1?
X is 1?
X is 1?
X is 1?
Sorry but this is an 😈😈infinite😈😈 loop so it just KEEPS printing it until something else happens.
So I can't show it all.
So that was a extremely easy part to explain.

Python operators

Source: https://www.w3schools.com/python/python_operators.asp


The operators are [Because of Markdown I can't show the asterisk] , - , + , / , // and [Because of Markdown I can't show TWO asterisks]
'+' adds
'-' subtracts
'[I can't show the asterisk sign but it basically looks like a star]'
multiplies
'/' divides
'[This sign is basically 2 asterisks]' exponentiates [x to the power of y]
'//' rounds the answer to the nearest whole number if it is a decimal
These ones I don't know what they do[If you know what they do please tell me in the comments!]:
'&' ???
'<<' ???
'>>' ???
'|' ???

A calculator

This part might look hard, but there are actually three EZ ways to do it!
I'll start with the hardest and longest one first:


Way[1]

All of these ways have the SAME output:
In the first way, we use functions!
Input:

def add(x , y):
> sum = x + y
> print(sum)

def subtract(x , y):
> difference = x - y
> print(difference)

def multiply(x , y):
> product = x * y
> print(product)

def divide(x , y)
> quotient = x / y
> print(quotient)

x = int(input("Enter first number"))
y = int(input("Enter second number"))
operator = input("Enter the operator:")
if operator == "*":
> multiply(x , y)

elif operator == "+":
> add(x , y)

elif operator == "/":
> subtract(x , y)

elif operator == "-":
> subtract(x , y)

else:
> print("Invalid input.")

Way[2]:

In this way, we use lambda():
Input:

add = lambda(x + y)
subtract = lambda(x - y)
divide = lambda(x / y)
multiply = lambda(x * y)
x = int(input("Enter first number"))
y = int(input("Enter second number"))
operator = input("Enter the operator:")
if operator == "*":
> multiply(x , y)

elif operator == "+":
> add(x , y)

elif operator == "/":
> subtract(x , y)

elif operator == "-":
> subtract(x , y)

else:
> print("Invalid input.")
Way[3]

In this way, we just print it:
Input:

a = int(input("Enter first number"))
b = int(input("Enter second number"))
operator = input("Enter the operator:")
if operator == "*":
> print(x * y)

elif operator == "+":
> print(x + y)

elif operator == "/":
> print(x / y)

elif operator == "-":
> print(x - y)

else:
> print("Invalid input.")
Output[same_for_all_of_them]:

Enter first number13
Enter second number13
Enter the operator:*
169
[Put whatever number you want for the first number, the second number and put whatever operator you want!]
So that was the tutorial!
Thank you for being patient!
I hope you liked it!
Please put any questions or comments in OBVIOUSLY the comment section below.
Bye!