Beginners Python Tutorial!
Today I will be teaching you
To those of you who say there are too many python tutorials just for cycles, I wholeheartedly agree. I am not just doing this for cycles. I want to help people. This is purposely for beginners and it is supposed to only cover the basics.
Table of contents
- Multi-line printing
- User Input
- Calling Functions
- Basic Operators
- Basic Math Using Operators in Variables
- Multiplying lists
- Basic Math Using Operators in Variables
- Top 5 Best modules to import
So this is really basic. This is the first thing that I learned in
print('') and it will print it to the console. Example:
print('I am printing to the console!') # Replace the sample text
This is basically the same as printing but it writes the text letter by letter. you need this code for it to work:
import time import sys def write(str, eol = "\n"): for char in str: time.sleep(0.1) # You can change this to any time you want, but th e longer the time the longer the text takes to appear. sys.stdout.write(char) sys.stdout.flush() sys.stdout.write(eol) sys.stdout.flush() # Wanted to mention use write('') instead of print('') write('Animated text ;)') # Sample text. Change it.
Thank you to @KierstenKuk for this on their repl called Detective's Case! Also just check out their profile too. They are an amazing coder! (Check out their Profile.) By the way, from this point on I won't use
write(''). I will still use print('').
This is pretty simple. All you have to do is add six quotations or (
'''ex'''). Write in the middle of them. This is if you want to add the key
enter into the printing. Or if you want to add an apostrophe. (and other things) If not you would get this error:
SyntaxError: EOL while scanning string literal Example:
print('''This is a multi-line string.''')
I also think that this is worth mentioning here: if you also want a multi-line string using a different way you could use
\n. What this means is that it is a line break. Example:
print('This \n is \n a \n multi- \n line \n string.')
input('ex') (Change ex to what you want.) To have a user
input lets the user type and have a response. (Also, you could use
ex = input to name one. [Replace ex] )
Challenge: Make a simple quiz game using
if == '':, and
In order to make the input actually work you need to add if '':. If you wanted someone to answer a question then you use
if == "yes": and
if == "no":. You can use
else: to determine if the user has made an input that is not defined by the
if == "":.
tomato = input('''1st question: Do you like tomato sauce? (not on pizza)''') if tomato == "yes": print("Incorrect") if tomato == "no": print("Correct") else: print("Answer with a correct statement.")
For functions, you can use
def. You can put what you want to happen underneath. This might be confusing. Example:
def sample(): print('sample')
For this all you have to do is call the function name. so mine was
sample() so i have to type in
def printfromfunction(): print('This is printed from a function') printfromfunction()
This is printed from a function.
You don't only need to put a
def is valid and you put the name of the function with closed parenthesis then anything would work. That is just my example.
So in order to define a variable you have to do
sample = 4 that is a variable. You can also use quotations to define a variable as a word. Like this:
sample = 'example' you can change the number and the example. If I used:
sample = 'I am printing using variables.' print(sample) sample2 = 726467626 # This is a hidden code. Decipher it and I will give you a shoutout. Hint: Keypad ;) print(sample2)
I am printing using variables. 726467626
You can make a list in python with the following syntax:
ex = [1,2,3,4,5,6,7,8,9,0] # Lists with numbers ex2 = ["ex1", "ex2", "ex3"] # Lits with letters print(ex) print(ex2)
0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0] 'ex1' ,'ex2' ,'ex3'
You can call these lists in any way you want, you can use them to list
Challenge: make a calculator using basic operators. (Using input might be a good idea).
Basic Math Using Operators in Variables
You can make a variable using math. If you want to use addition use +. If you want to do subtraction use -. If you want to use multiplication use *. If you want to do division use /. Use two starts/multiplication signs to do to power. Example:
addition = 1 + 2 print(addition) subtraction = 3 - 1 print(subtraction) multiplication = 2 * 10 print(multiplication) division = 20 / 10 print(division) power = 2 ** 7 print(power)
3 2 20 2.0 128
Adding words to the console using +
You can use a variable and make it have this syntax:
ex = 'This' + '' + 'is' + '' + 'an' + 'example.' print(ex)
This is an example.
What this does is it adds words together using the + sign. It adds these words together:
'This' + 'space' +'is'+'space'+'an'+ 'example.' and it prints it to the console.
This is an example.
Multiplying Strings to Make Multiple of the Same Sentence
This is pretty easy. Make a variable called whatever you want and then add a multiplication sign (*) to whatever you want to multiply. Example:
lotsofex = 'a lot of times' * 10 print(lotsofex)
a lot of times a lot of times a lot of times a lot of times a lot of times a lot of times a lot of times a lot of times a lot of times a lot of times
Multiplying using lists
You can multiply with strings following this syntax:
ex=([1,2,3] * 2) print(ex)
With this what you are doing is 123x2. The output might make it easier to understand.
[1, 2, 3, 1, 2, 3]
Top 5 Best Modules to Import
#2 Wikipedia. I do not even know for this one. You can literally just import the whole Wikipedia. For more information and its syntax visit here.
#3 Time. My favourite use for time is
time.sleep(3). You can change the time inside the parenthesis. That stops the next line of code for how many seconds you put inside the parenthesis. For more info about time and syntax visit here.
#4 OS. My favorite use for os is
os.system('clear'). What this does is clears everything in the console. For more information about os and its syntax visit here.
#5 Turtle. Turtle is a module that lets users draw. This is one of my favourite modules. For more information and syntax visit here.
I hoped that you like my beginner's python tutorial! If you like it please upvote.