Learn to Code via Tutorials on Repl.it!

← Back to all posts
~~~Python Basics Detailed Tutorial~~~

# What we will be covering in this first course:

• What is Python?
• What are its uses
• What is its demand?
• Where can you code in Python?
• Variables, print statements, strings and formatted strings.
• Numbers, Floats, Arithmetic Operations
• User input.
• Booleans
• and & or
• Lists

# What is Python?

Python is an easy to learn language due to its very simple syntax. Made by Guido van Rossum in 1991, Python has since risen to become one of the best and most used languages in the world. Companies like Facebook, Netflix, Instagram etc. use Python in there software. Reddit is an app entirely made from Python.

# What are its uses?

Python is widely used for:

• Web and Internet development
• Desktop GUIs
• Software Development

There are other languages which are much better for making games but Python is widely used for the other reasons mentioned. Text Based Games are generally best made using Python.

# What are its demands?

Large companies like FANG (Facebook, Apple/Amazon, Netflix, Google) are hiring Python developers as their demand is really high. The average salary of a Python developer is \$80,000/y and if you are highly experienced, your salary may go up to about \$110,000/y.

# Where can you code in Python?

To interpret Python code, you are going to need an IDE (Integrated Development Environment). There are many Python IDE's out there but the most common ones are:

# Variables, print statements, strings and formatted strings.

Now, lets begin actually learning Python.

Variables in Python are used to store date. For example:
`age = 30`

The variable "age" is storing the value "30". So Python now knows that the value of "age" is "30". There are more ways we can implement variables.

We can put a number right after the text without a space:
`age1 = 15`

We can separate the second word with an underscore `_` :
`my_age = 18`

Or just capitalize the second word:

`myAge = 18`

Print statements in Python are used to display code on the console which is the place where all the code is executed. We implement print statement like this:

``````age = 30
print(age)``````

Output : `30`

But if we need to print something that is not associated with a variable, we have to use strings.

These are the types of strings in Python :

• Single strings : `print('This is a single string')`
• Output : `This is a single string`

• Double strings : ` print("This is a double string")

• Output : `This is a double string`
• Multiline strings :
``````print("""This is a multiline string

Which means you can use as many lines as you want
""")``````
• Output:
``````This is a multiline string

Which means you can use as many lines as you want``````

• You can print also have as many lines as you want in one line of code by using "\n". Like this:
``print("You can use as many lines in one line of code.\nLike this\nAnd this\nAnd this\nI better stop here")``

Output:

``````You can use as many lines in one line of code.

Like this

And this

And this

I better stop here``````

• Formatted strings can be used to print something using a print statement and also using a value associated in a variable in the same line. For that we have to use 'f' before opening a string and use '{}' to use the variable inside the string.. Like this:
``````age = 18
print(f"I am {age} years old")``````
• Output:
``I am 18 years old``

# Numbers, Floats, Arithmetic Operations.

Numbers and floats are pretty simple so I will go through them real quick.

• This is a number : `56`
• This is a float (which is basically a decimal): `4.906`

#### Arithmetic Operations.

Arithmetic Operations in Python are similar to those in Math.

• Addition: `print(2 + 2)`
• Output: `4`

• Subtraction: `print(7-4)`
• Output: `3`

• Multiplication: `print(3*2)` --- Notice that we use '*' for multiplication.
• Output: `6`

• Division: `print(12/4)` --- Notice that we use '/' for division.
• Output: `3`

• To print out only the remainder of division, we use "//" or "%": `print(f"{6 // 5} \n{12 % 5}")`
• Output:
``````1
2 ``````

• We can use '>' for greater than and '<' for smaller than and '>=' for greater than equals to and '<=' for smaller than equals to.

# User Input.

When working with Python, we need user input to actually gain information and act in accordance with the user's choice. For example with have this bunch of code:

``````my_name = "ABRJ"
user_name = ""

print(f"My name is {my_name} and the user's name is {user_name}.")``````

If we run this code we will get an output like this:

``My name is ABRJ and the user's name is  .``

This happens because we don't known the user's name and is set to an empty string.
To get user input, we use `input`. To do that we simply put an input behind our question like:

``````my_name = "ABRJ"

print(f"My name is {my_name} and the user's name is {user_name}.")``````

So for example if the user inputs "John", the output would be:

``My name is ABRJ and the user's name is John .``

# Booleans

Booleans are True or False values that are used to make decisions. For example, is the user's age over 18 or below. That is a comparison that can be evaluated to True or False. The keywords in Python are in Proper case:

``````truthy = True
falsy = False``````

These two variables are pointless on their own. But if we have a number like:

``age = 18``

we can use to evaluate to True or False:

``````age = 18
is_over_age = age >= 18``````

The `age >= 18` is a Boolean expression.
We can also have is_under_age:

``````age = 25
is_over_age = age >= 18
is_under_age = age < 18``````

So here, "is over_age" is True because the age is 25 (which is more than 18) where as "is_under_age" is False because the age is under 18.

#### What is the use of Booleans then?

We can use Booleans to make decisions of True & False. For example if the Boolean is outputted as True, we run a specific program. And if it is False, we run something else.
Example:

``````my_number = 5
user_number = int(input("Enter a number: "))

print(my_number == user_number)``````

If we run this program, and the user inputs 5, the program will give out "True" and if it is ANY other number, it will give out "False".

### and & or keywords

Now that we have talked about Boolean expression, we can go in depth and discuss and and or keywords in Python. To have an understanding of these keywords, lets start by asking the user for their age to see if they are old enough to learn programming [There is actually no age limit to learn how to code :) ]:

``age = int(input("Enter your age: "))``

Now lets make a variable:

``````age = int(input("Enter your age: "))
can_learn_programming = age > 0``````

Here we have created a Boolean comparison that the age should be greater than zero. But sometimes users can be to old to actually keep information in their brains so we have to add another Boolean comparison. For that we use the "and" keyword:

``````age = int(input("Enter your age: "))
can_learn_programming = age > 0 and age < 150
print(f"You can learn programming : {can_learn_programming} ")``````

So when the user inputs an age that is above 0 and below 150, the program is going to give out "True". Where as if the input does not meet these requirements, the program will print out "False".(I used the age 150 because I don't think anyone has been as old as 150)

We can use "or" keywords like:

``````age = int(input("Enter your age: "))
usually_not_working = age < 18 and age > 65

print(f"At {age} you are usually not working : {usually_not_working} ")``````

So when the user inputs an age that is under 18 and below 65, the program is going to give out "True". Where as if the input does not meet these requirements, the program will print out "False". "Or" is used when a person can have either of the requirements, whereas and is used when both requirements have to be met.

# Lists

As our program grows in Python, working with variables starts to get harder. For example if we have three friends:

``````friend1 = "John"
friend2 = "James"
friend3 = "Johnny"``````

Imagine we had over twenty friends (which is rare?) and making a variable for each one of them would get overly-complex. If you wanted to printout the name of all your friends, print statements are going to get repeated over and over and over again. That is where lists come in. We create a list by defining a variable and putting a pair of square brackets around our items separating them buy commas:

``friends = ["John", "James", "Johnny"]``

Now to access the first item in the list, "John", we can simply do:

``print(friends[0])``

Notice that we don't have to put strings while printing and to access the first item in the list we put the index 0 surrounded by "[]". For the second item we use "[1]" and for the third "[2]" and so on.

To find the length of the list, simply use the function "len":

``print(len(friends))``

Which is going to give out '3' as there are three elements in there.

To add another element, we can use the "append" keyword:

``print(friends.append("Mark"))``

Finally, to remove an element, use "pop" and the index of the element:

``print(friends.pop(1))``

Which is going to print out all except the second element in the list.

### Nested Lists

For example if we have a list of my friends and their ages we can use lists inside lists:

``friends_n_ages = [["John", "15",], ["James", "18",], ["Johnny", "16"]]``

To access "John" and his age we can acces the first element in friends_n_ages and the second element in that:

``print(friends_n_ages[0][1])``

JBYT27 (1314)

...Why are there so much Python tutorials these days?

sigh...

OldWizard209 (321)

Large companies like FANG (Facebook, Apple/Amazon, Netflix, Google) are hiring Python developers as their demand is really high. The average salary of a Python developer is \$80,000/y and if you are highly experienced, your salary may go up to about \$110,000/y. ;) @JBYT27

Wumi4 (460)

Here are some fun stuff:

• You can also use multiline string for commenting on Python!
``````"""
This is
A multiline comment!
"""``````
• The f-string (`f""`, the one that he used in this tutorial) won't work in some cases (like for binary formatting) so the old `.format()` one is a better solution in some cases. They all work the same!
``````age = 18
print("I am {} years old!".format(age))
# Output: I am 18 years old!``````
• Only for advanced Python coder: You can also say what types do you want to return from a function(You will learn about it when he releases a new tutorial!) using the `->`(or the thin arrow symbol)
``````# Here, we have a sum function
# that returns a int!
# If you are a complete beginner,
# skip this.
def sum(x, y) -> int:
return x + y``````
OldWizard209 (321)

I am going to talk about this, But in later tutorials :) But thanks for the info . @Wumi4

Th3OneAndOnly (6)

@Wumi4 make sure that if you teach someone type hints that they don't go and start type hinting every local variable. It looks ugly to every experienced python coder and is just unessescary. I reccomened type casting function declarations and class attributes and using mypy for just that.

Th3OneAndOnly (6)

@Th3OneAndOnly and do note that type hinting is NOT type casting

supcoolak (8)

Wait, I love Atom too, Atom owned by GitHub. It's also very popular for coding in Python. Sorry if it sounds like advertising, I used atom a lot before I discovered repl.it.

Th3OneAndOnly (6)

@supcoolak I love Atom, for many it's just too much setup but I love how I can customize it so it's interface seems like it's crafted just for me -- which it is! I guess... anyways happy to see another Atom lover!

supcoolak (8)

@Th3OneAndOnly :), also a nice tutorial, I never new that Reddit was based on Python.

Bookie0 (5090)

Why is the title almost the same as mine lol (this one)?

also you should try to add more stuff as the basics include conditionals, functions, etc.

You should also use syntax highlighting by adding `py` after the 3 backslashes (`):

OldWizard209 (321)

About the title: Must be a coincedence. And about the basic tutorial, I will release another post soon talkin about everything until (O.O.P) @Bookie0

Bookie0 (5090)

You should try to put all the tutorials together instead of making a series =/ :)

OldWizard209 (321)

Is cuz I am lazy..... :/ @Bookie0

Bookie0 (5090)

@abdullahrajput9 that's a...terrible excuse lol

Bookie0 (5090)

you just have to instead of making a different post, just edit this one and copy it in here! :) @abdullahrajput9

DSAEvan (16)

everyone says "... made easy" lol, but good tutorial! how about creating a python-learning repl?

OldWizard209 (321)

Sure umm, you mean like a repl where all the code I used is displayed? @DSAEvan

AlphaPro69 (1)

Thanks!! Helped me a lot since I am a beginner. @abdullahrajput9