Learn to Code via Tutorials on Repl.it!

← Back to all posts
✨✨The Python Tutorial✨✨
IntellectualGuy

✨✨Hello✨There✨✨

Welcome to My Boring Fun 🐍🐍Python🐍🐍 Tutorial where I will be teaching YOU python. This tutorial will cover the basics of python an I promise you that you will learn a Lot. Without a Further a due go and learn Python! Credits to @RhinoRunner for helping with the tutorial. (Please don't think I am cycle squeezing, this tutorial took me around 1000 lines of markdown). I have attached a repl down below that is a copy of this tutorial(It's split up into 3 files, because the file became to large for our computers to handle.)

Note: Feel free to tell me if something is unclear or if it isn't correct, or if there is anything I can add.

Edit4: Since this is so popular I will resume making part 2, sorry for the 5 month delay lol. And since I want to get part 2 up soon if anyone wants to help then just comment on the post.

Course Content

Before I get started here is the Course Content

  • Comments
    • Single-Line
    • Multi-Line
  • Print Statements
    • Single-Line
    • Multi-Line
  • Data Types
    • Strings
    • Integers
    • Floating Points
    • Booleans
  • Variables
    • Proper Variable Names
    • Printing them
    • Changing them
    • Type Function
    • f-strings
    • .format
  • String Methods
    • indexing
    • slicing
  • Concatenation
    • Printing Using Concatenation
    • Assigning Variables Using Concatenation
    • Type Casting
  • Operators
    • Basic Operators
    • Assignment Operators
    • Comparison Operators
  • Getting Inputs
    • Storing inputs in a variable
    • Specifying input type
  • Practice Problem
  • Lists
    • List Methods
  • Conditional Statements
    • if
    • else
    • elif
    • pass
    • logic
  • Practice Problem
  • Loops
    • for Loops
    • while Loops
  • Practice Problem
  • Escape Codes
  • Functions
    • Return
    • Parameters
    • Lambda
    • Scope
      • Global
      • Local

Let's Get Started!

Comments

Single-Line Comments

Comments are blocks of code that are ignored by the computer and can be used to stay stuff about your code. The way to tell your computer that you are entering a comment is with a #.

So here's an example:

Multi-Line Comments

You can also have comments that span multiple lines which are done with three single or double quotes to start and 3 to end.

Here's an example:

Comments are mostly used when you want to say something about how your code works, so when you revisit your code you aren't confused on what you were doing. Another common use is when you want to leave some code out of your program and not have it run without completely removing it from your code.

Printing

The print() function prints(or outputs) something to the console.

Single-Line Printiing

Here's an example:

This would output

Multi-Line Printing

If you want to print text for multiple lines then use 3 single quotes or doubles quotes to start and 3 to end.

Here's an example:

The Output:

Data Types

str or string is a data type that is a sequence of characters and is surrounded in double or single quotes

Ex: "Python Rocks",'Python is Fun'


int or integer is a any positive or negative number (without decimals)

Ex: 5,69,-568


float is any positive or negative number with a decimals

Ex: 69.69,5.0,-15.89


bool or boolean is a True or False(It can only be True or False) value that can be used for Logic (Make sure to capatalize it)

Variables

Variables are data types that are assigned to variable names and are used to store information.

Proper Variable Names

They can use numbers,letters,and underscores, but can't start with numbers. No spaces are allowed either(Use underscores instead). You should use variable names that relate to the value being stored, and variable names that are also short.

Here are a few example of valid and good Variable names:

Printing Variables

You can also print variables by putting them in the parenthisis of the print Statements

It would output IntellectualGuy

Reassigning Variables

You can also change the value of the variable by simply just reassigning it

You can also switch the data type

Type Function

After you change a variable a lot you might want to find out what data type it is so you just use the type() function.

It would output <class 'int'>, which basically means that the data type of the variable apple is an int or integer.

F-Strings

F-Strings print out stuff with a variable in between. This will become more useful when you learn about inputs.

Here's an example:

It would output:

.format()

Another Way to do that is using the .format method which you use like so

Output:

String Methods

There are many different string methods that you can use. String methods are basically functionsthat you can apply to your strings. FOr example the print() function that I explained earlier can print a string to the console.

Something to know before starting string methods it that each character in a string has an index value, starting from 0.

Here's an example

Index positions can also be in the negatives like so

Negative index positions are mainly used for things like getting the last character of a string

Indexing

Indexing is getting the value of a certain index of a string, and you use it like so

Output: l

You can also use negative indexing to get a character

Output:o

Slicing

Slicing is used to get a certain part of a string like so

Length

Length is used to get the length , the syntax is len(string) of a string and is used like so

Output:5

Upper

The .upper() method is used to uppercase a string like so

Output: BOB

Lower

The .lower() method is used to lowercase a string like so

Output: sam

Concatenation

Concatenation is when you add/join 2 or more strings together.

For example:

Output: You are the Admin.

You can also assign a variable using Concatenation

For example:

Output: You are a Guest

Remember you can Concatenate more than 2 strings together

For Example:

Output: Hello Admin What would you like to do?

Type Casting

You can't Concatenate strings with integers/floats/booleans, you would have to cast the type, let's say you wanted to print out a message with a number using Concatenation, then you would use tyoe casting, which let's you change the type of a variable.

Ex:

Note: Type casting only temporarily changes the data type of the variable, not permanently, If you wanted to though then you could assign the variable to the casted variable like solution

Operators

Basic Operators

You can do basic Math with Python.

Addition - You can add Two numbers with a plus sign +
Ex: 5 + 5 , 6.9 + 9.6
Output: 10 , 16.5

Subtraction - You can subtract Two numbers with a plus sign -
Ex: 10 - 5 , 9.6 - 6.9
Output: 5 , 2.7

Multiplication - You can multiply Two numbers with an asterik *
Ex: 5 * 5 , 5.5 * 3
Output: 25 , 16.5

Division - You can divide Two numbers with a forward slash /
Ex: 50 / 5 , 20.4 / 4
Output: 10 , 5.1

Power - You can get a power of a number with 2 asteriks **
Ex: 2 ** 3
Output: 8

Modulo - You can get the remainder between Two numbers with a Percentage sign %
Ex: 69 % 6 , 25 % 5
Output: 3 , 0

Floor Divison: Ignores decimals when doing Division with two forward slashes //
Ex:10 // 3,17 // 4
Output: 3,4

You can assign variables to use them for math
Ex:

Output:

Assingment Operators

Assignment operators are used to assign variables using operations. Here is a list of them.

+=
-=
*=
/=
%=
//=
**=

Now let's have a few examples of using them

Output:

The Assignment operators are technically just shortened down Basic Operators. For example a += b is just saying a = a + b,it's just another way to write it, however I still recommend using the shortened version.

Comparison Operators

Comparison operators are used to get a true or false value. Here is a list of them

Checking if a value is equal to another value. If it is equal to the other value then the it is True. Otherwise if the other value is not equal to the value it is False - ==


Checking if a value is not equal to another value. If it is equal to the other value be compared then it is false. Otherwise if the value is not equal to the other value then it is False - !=


Checking if a value is greater than another value. If it is greater than the other value then it is True. Otherwise if the value is less than or equal to the other value then it is False - >


Checking if a value is less than another value. If it is less than the other value then it is true. Otherwise if the value is greater than or equal to the other value it is False - <


Checking if a value is greater than or equal to another value. If it is greater than or equal to the other value then it is True. Otherwise if the value is less than the other value then it is False - >=


Checking if a value is less than or equal to another value. If it is less than or equal to the other value then it is True. Otherwise if the value is greater than the other value then it is False. - <=


Something to remember is that there are opposite comparison operators like == and != are opposite value because == is checking if the values are same and != is checking if the values are different.

Opposite Pairs:

== and !=
> and <=
< and >=

Getting Inputs

You can also get the user to input something using the input() function, here's an example of using it:

Output

It would output How old are you and then I could say whatever my age was.

Storing inputs in a variable

You can store the input that you get from a user into a variable like so

Output:

How old are you: 69

Remember it doesn't print 69, I am just using 69 as an example input.

Specifying input type

You can also specify the data type of the input like so

Output:

How old are you: 69

Practice Problem 1

Small Exercise

Try to make an multiplication calculator where you ask the user to input 2 numbers and then output the product of the 2 numbers

Solution:

Lists

Lists are one of the more complex python data type. Lists hold other data types like strings, integers, floats, and even lists(We will go over nested lists in the next tutorial). They are declared using this syntax

Each list item has an index, like strings. The index starts from 0.

Here's an example to show

Lists also use negative indexing

List Methods

There are many methods that you can use with lists.

The .append() method adds an item to the list

Example:

Now fruits is ['apple','bannana','orange','mango']

The .pop() method removes an item from a specific index

Example:

Now fruits is ['apple','bannana','orange']

The .remove() method remove a certain item from a list

Example:

Now fruits is ['apple','bannana','mango']

The del method can delete a whole list or a specific index of a list

Example:

Now fruits is ['bannana','orange','mango']

Another Example:

Now there is no fruits list.

Conditionals

Conditionals can be used to create code that will run if a certain condition will used, the syntax is like so

If you don't specify if you want the code to run if the condition is true or false then the default is that when your code is run, if the condition is true then the code will run.

Here's an example of a valid if statement using comparison operators

Output:

Another Example

Output:

Elif

Elif is to have multiple conditions that can check if something is true if the if statement is not true. You can have as many elif statements as you want.

Example:

Output

Another Example

Output:

Else

Else conditions, are run when all of your if or elif statements are run, and haven't been executed.

Note: In if,elif, and else statements, in a single conditional, only one condition will run. So let's say you have multiple conditions, and 2 of them are true. The one that was stated the first will be run, and the second one won't be run.

Here's an example

Another Example:

Output:

Remember the reason it won't output "Your name is Bob" is because only one statement will run in a single conditional statement.

pass

The pass keyword is used as a placeholder so when you have a if statement that you want nothing to execute if that condition is true then use the pass keyword

Example:

This wouldn't output anything because nothing happens in the if statement because there is a pass

Logic

You can have multiple conditions in each if statement, using the and keyword and the or keyword.

When you used the and keyword then the code will only run when all conditions are true.

When you use the or keyword then the code will run if one or more of the conditions are true.

Practice Problem 2

Small Exercise

Try to make an odd and even checker, where you get a number from user input and then the computer will check if the number is odd or even. If it is odd then print out the number and say that it is odd, and if it is even then print out the number and say that it is even. Go make a new repl and try it out! If it becomes too hard then feel free to check out the solution.

Solution

Loops

Loops are used to run things multiple times, and there are 2 main types, for loops and while loops

For Loops

For loops are used to iterate through items in a list, dictionary, integer, or string. They can be used to do something for every item in a variable.

Example:

output:

As you can see, it looped through every value in the list example_list.
The reason the i is there is to have a variable you can assign to the thing it is looping through. You can change i to any other name as long as another variable doesn't have the same name.

Another thing you can use is range.

output:

It starts from 0 and goes to the number before the number you said, so it has 10 numbers in it.

If you want to exit a for loop, you can use break.

output:

As you can see, when the number reached 5, the loop ended.
You can also skip iterations using continue.

output:

When the loop reached 5, it skipped the print statement and went back to the beginning of the loop.

While Loops

While loops are used to run a program while the variable stated is the condiion stated.

Example:

output:

So, the loop keeps going on so long as num is greater than other, it prints other. But, when they were equal (both at 6), the loop stopped.

You can also use break and continue in while loops. It works the same as it did in the for loops.
The most common usage for while loops is with bool values.

output:

Since var is never changed (it always stays true), the loop goes on forever.

Practice Problem 3

Make a program where for every number from 1-100, if the number is divisible by 15, it would print Fizzbuzz, if the number is divisble by 5, it would print Fizz, if the number is divisble by 3 then bring Buzz, otherwise it wil just print the number.Then Ask the user If they want to start the program again, if yes than restart it again, otherwise break out of the loop. Go make a new repl and try it out! If it becomes too hard then feel free to check the solutions.

Challenge: Ask the user the maximum number they want the program to run to.

Solution:

Challenge solution:

Escape Codes

There are multiple escape codes that you can use in your code to do different things

\n This prints the text on a new line.

Ex:

Code:

Output:

\t This prints a tab.

Ex:

Code:

Output:

Functions

A function is a block of code that runs code. They are mainly used when you want to use code again and again, without having to write it multiple times.

Here is the syntax for it

Here is an example:

The way that you use or call functions is through this syntax

Output: 10

And you can use the function multiple times.

return

There is a special keyword that can be used in functions and it is the return keyword

What the return keyword does, is that it gives the function that you call a value. And you have to print the function out when you want the code that was run

So here's an example

You might think that using try_func() would print out 5, but really it would assign the value of 5 to the function. If you wanted it to print out 5, then you would have to do print(tryfunc())

Parameters

Parameters are something used in a function to make it more versatile and interactive. Parameters are variables, that you pass in when trying to call a function.

Here's the syntax:

Syntax to run it:

Here's an example of a function using paramters

this should output

Basically, the function is just saying, give me two numbers, and I'll add them and return the sum.

Lambda

Lambdas are short functions that you can use in your code, and are assigned to variable names.

Here's the syntax

Calling syntax:

And here's an example

Calling it:

Output: 25

You can also use lambdas in function, the are mostly used in the return

Output:Programer

Scopes

A scope is where a variable is stored. Like if it's inside if a function.

Global

The global scope are variables that can be accesed by all of your code.
eg.

Local

The local scope is the scope of a function, and you may notice, that when you set a variable inside of a function then you can't access it outside the function, and you will get an error. This happens because the version of that variable only changes inside of the function. Here is an example of the error.

The way you can fix this is by using the global keyword and declaring that variable as a global variable

The global keyword basically moves the x variable from the local scope to the global scope, and all changes that are made when the function is called is applied to the global version of x.

Conclusion

Wow, did you actually read all of that, or did you just skip to the ending?
That was (pretty much) all of the python basics!
If you want a further explanation on something, or feel as if we missed something, put it in the comments and we will make revisions!

MAIN CONTRIBUTOR: @IntellectualGuy

PARTIAL CONTRIBUTOR: @RhinoRunner

Credit for a typo - @mitiok

Voters
DhruvSiva
kim-Jong-yuhJon
shivamgadekar
WaselAhmed
robcs9
scottedwards200
motraor3
HunterLewis2
Isamp
murrieta02
Comments
hotnewtop
[deleted]

Imma upvote bc

• This probably took ages
• It's very helpful
• I like it :)
• I'm nice
yuh

JUST REALISING THAT I UPVOTED YESTERDAY-

pythongeek1010

@Rainbowstuff Lollllllll XD

MustafaShazlie

IntellectualGuy

@MustafaShazlie lmao, glad you like it

ch1ck3n

im not gonna upvote

why?
there are a thousand python tutorials in the repl talk already

IntellectualGuy

@ch1ck3n I am sorry that you don't like it, but I will be adding functions to it shortly.

ch1ck3n

@IntellectualGuy no its not that i don't like it, but there are too many python tutorials out there.

IntellectualGuy

@ch1ck3n Ok, it's not just you multiple other people have been saying it's trash.

ch1ck3n

@IntellectualGuy ok since I like this one because it's actually good I will upvote

zplusfour
Maudie13

IKR lol @zplusfour

JDJGInc_Offical

@ch1ck3n Look, I know python and for sure I can tell you this is on of the good tutorials.

ch1ck3n

@JDJGInc_Offical I saw the word "scopes" and i knew it was good

codingjlu

@IntellectualGuy this isn't trash it's great but wayyyyy tooooo many Python tutorials.

ShiftyBoo

@ch1ck3n How many HTML tutorials are there? I'm hoping to make one, but I don't want to be repetitive if there are a lot.

ch1ck3n
ShiftyBoo

@ch1ck3n So in other words, yes I will be repetitive if I make one.

ch1ck3n
ShiftyBoo
Sleek

this deserves more likes

IntellectualGuy

@Sleek thx

Sleek
[deleted]

@Sleek likes = NNNNNNNNNNNOOOOOOOOOOOOOOOOOOOOOOOOOOO! UPDOOTS FOREVER

OldWizard209

And BTW, you need to credit Udemy for The FizzBuzz challenge. I pretty sure that is from the Python course which I bought on Udemy. You may have bought it too. So PLEASE credit them... @IntellectualGuy

IntellectualGuy

@OldWizard209 It's a very common thing that is used, but sure I will. Also I see you haven't upvoted, anything wrong?

RhinoRunner

@IntellectualGuy I mean, not everybody has to upvote.

IntellectualGuy

@RhinoRunner I know, I'm just asking if there is anything wrong, not asking for upvotes.

OldWizard209

Sorry, Lol I upvoted and closed the tab quickly and there were many apps running so the processing speed might be slow. I will upvote agiain. @IntellectualGuy

IntellectualGuy

@OldWizard209 You didn't have to, I was just asking if anything was wrong because I really want this tutorial to be good cause I spent a lot of time on it.

[deleted]

@OldWizard209 not from udemy, it's a common question for interviews

LuaIsGudScript

Thats like saying "And BTW, you need to credit Udemy for The Hello World challenge. I pretty sure that is from the Python course which I bought on Udemy. You may have bought it too. So PLEASE credit them..." @OldWizard209

vaytran

Love it !!!

IntellectualGuy

@vaytran tysm!!!

AshLikes2Code

Cool!

AshLikes2Code

Cool!

IntellectualGuy
Andrewsmith291

Thanks for the Post, I am learning Python and this is very useful for me.

IntellectualGuy

@Andrewsmith291 Np, I have a part 2 coming up soon that has more intermediate concepts.

[deleted]

This is really helpful in making my jaeger :D. I plan to teach others python while doing this, so this is like the meaning of life for me.

InvisibleOne


hmms...

IntellectualGuy

@InvisibleOne I swear I don't know how in the world that's there

InvisibleOne

I think it's just some crazy glitch @IntellectualGuy

CookieSnowOwl

Bro if u hate dis then ya gotta try doing one yourself. Python tutorials are hard to make. I've tried to make one................... I went way too fast. I talked about printing then skipped to like functions then like modules.... it's not ez

FlaminHotValdez

@CookieSnowOwl I'm not saying it's difficult, I'm saying it's overdone, redundant, and unnecessary.

ShiftyBoo

@FlaminHotValdez I agree. too many python tutorials on the internet

cannonthepom123

im making a text adventure game with this and am trying to make an ascii house
/\
| 0|
but it won't let print the '/' symbol
how would I do that?

IntellectualGuy

@cannonthepom123 You would do \\, because \ is a special character.

cannonthepom123

@IntellectualGuy
ooohh, ok thx

[deleted]

Epic tutorial I can see a lot of effort has been put into this

IntellectualGuy
RinUnderscore

YOU NEED TO DO THIS FOR EVERY LANGUAGE THIS IS AMAZING
wow how long did this take you

wowadino1

Very helpful! Thank you!

NARUTO12456789234567

cool

tankerguy1917

Where was this months ago. This would have saved soooo much looking around the internet.
Edit: I just realized this was here months ago and I just didn't see it.

Jieun07

How to make .env file
It always fails for me
can u help?

IntellectualGuy

@Jieun07 There is no .env file anymore, go to the sidebar and go to secrets, that's the replacement for it.

JakeHu2020

I know Node JS.

Eyadistic

so this is my first ever coding but i cant do the multi-line code caz idont know what to press could someone help me?

IntellectualGuy

@Eyadistic Wdym by you can't do multi-line code?

Dunce

@Eyadistic Press enter.

AutomationKing

Great content

KhShahid

that is vary helpful for a beginner
also helpful for advanced coders
need js tutorial
also need html



this is was helpful thanks

Jurrasicbus1

Thx that was helpful

javeriaqureshi

Loving ittt!

AboveAverageJoe

Is there a way to bookmark this post for future reference?

Sukrat

really nice tutorial.
learned a lot...

OldWizard209

Just gotta say something.

Everyone can make a tutorial upt to functions.

But no one tries Classes because that is where all hell breaks lose..

FlaminHotValdez

@OldWizard209

Just gotta say something.

Everybody can make a tutorial about Python.

But no one tries C++ because they are too lazy to learn a new language.

no offense, just pointing out a flaw in the tutorials system

CliveFord

@OldWizard209 Hi, there's a great free course on https://www.futurelearn.com/ - Object-oriented Programming in Python - covers classes, objects, methods etc

ShiftyBoo

@FlaminHotValdez to be fair, I don't know python OR C++, but I do know HTML.

MusaIbn

This is definitely the best python tutorial I have seen on the web for free!, Really appreciate your hardwork!.
Thanks again!!

ZAGAMING

So good for beginners