Python 101: The Ultimate Python Tutorial for Beginners
Published: 2022-04-24 08:21:31

**Python** is a high-level language that was designed by Guido Van Rossum and released in 1991.

Over the years, Python has grown to be popular and tops many charts as it gains more users. It is termed as a Beginner-friendly language because of its simple syntax and how easy it could be to master it.

Python has become one of the most reliable programming languages in different fields of specialization. From *Data Science, Machine Learning, Artificial Intelligence* to *software engineering*; Python is one of the best languages for a beginner who aims to learn programming or add something cool to their portfolio. 

####What are some of the features of Python?
* Object-Oriented Language: Your python program consists of set of objects that you used to solve a problem.
* Easy-to-learn language: Python is a language that is easy to learn and doesn't put pressure on a beginner desiring to learn programming.
* Interpreted language: Python is an interpreted language. It won't require you to manually compile and execute your program; just write code and run.
* Free and Open Source language: Python is an open-source language that is free for everyone to learn and use.
* Huge Standard Library: Python has you covered with a cool standard library that supports various specialization fields like web dev and data science.

*This are just some of the features of Python programming language.*

####How to install and set-up Python:
You can download the latest version of Python for your PC or laptop from [here](https://python.org/downloads). Versions for Windows, Linux and MacOS are available. 

You can go through a guide on installation of Python on various OS platforms from [here](https://www.w3computing.com/python/installing-python-windows-macos-linux/) and also know how to configure Python for VSCode from [here](https://www.w3computing.com/python/configuring-python-features-for-visual-studio-code/).

*Note: There are various websites you can use to learn Python. For starters, I would recommend [Programiz](https://www.programiz.com/python-programming/first-program) and [GeeksforGeeks](https://www.geeksforgeeks.org/python-programming-language/) for learning the basics of Python and some advanced topics.*

####Commonly used IDEs for Python
There are two commonly-used IDEs that are beginner-friendly. They are:
*[VSCode](https://code.visualstudio.com/)
*[PyCharm Community Edition](https://www.jetbrains.com/pycharm/download/)

####Writing your first Python program: Hello World!
This first program is very easy and shows a portion of what will be dealt with. Check out the code:
```python
print("Hello World!")
```
The code consists of print() which is a built-in function used to print expressions(strings, numbers, etc.) written within parentheses and "Hello World!" string.
The code output after running the program is:
```
Hello World!
``` 
####Comments 
Comments are used as to describe what happens at some point in the code of a program or to leave a message for another developer modifying the code. It's very meaningful in a code-space. Python comments have their own syntax:
```python
#This is a single-line comment

"""This is a
multi-line
comment, usually referred to as Docstring"""
```

####Variables
Declaring variables in Python isn't as complicated or very precise like in C++ where you specify the data-type before declaring your variable. 

```python
name = "John Doe"
age = 21
```

As displayed in the code block above, you declare a variable by simply declaring a variable name and add a value to it and you're set to proceed.
The output:
```
John Doe
21
```

*When writing strings, you could use single quotation marks(' ') or double quotation marks(" "). Both work perfectly.*

####Taking input from user
Take for example, you want to create a program that takes a user's name and age and returns a statement welcoming them. You will need to take input from the user interacting with the program. You can do that with ease as shown below:
```python
name = input("Enter your name: ")
age = int(input("Enter your age: "))

print("Welcome,", name, ". You are", age, "years old!" )
```
We declare the variable 'name' and request the user to enter their name. The same is done for 'age' but the data-type is specified because the input() function takes strings by default.
After the user inputs their name and age as 'James Doe', 30, the output becomes:
```
Welcome, James Doe . You are 30 years old!
```

#####Concatenation of strings with variables
Concatenating of strings with variables can be done in various ways. The variables used in **Taking input from user** shall be used to explain concatenation
1. Use of the addition (+) operator:
```python
"""While using addition operator to concatenate, convert 
all int variables to string with str() function"""

print("Welcome, " + name + ". You are " + str(age) + " years old!")
```
Output:
```
Welcome, James Doe. You are 30 years old!
```

2. Use of comma(,)
```python
print("Welcome,", name, ". You are", age, "years old!")
```
Output: 
```
Welcome, James Doe . You are 30 years old!
```
####Python Operators
Python operators serve different purposes as listed below:
*Arithmetic*: These operators are used to perform arithmetic operations in Python. They include: +, -, //, %, **, *, /
```python
print(5 + 2) #addition
print(5*7) #multiplication
print(2**5) #exponential
print(18/2) #division
print(3%4) #modulus
print(25//5) #floor division
print(5 - 4) #subtraction
```
Output:
```
7
35
32
9.0
3
5
1
```
*Comparison*: These operators are used to compare two things and return a boolean result. They include: >, <, <=, >=, ==, !=
```python
x = 20
y = 10

print(x < y) #less than
print(x > y) #greater than
print(x <= y) #less than or equals to
print(x >= y) #greater than or equals to
print(x != y) #not equal to
print(x == y) #equals
```
Output:
```
False
True
False
True
True
False
```
*Logical*: These operators are used to combine conditional statements. They include: and, or, not
```python
x = 20
y = 10

print(x < 30 and y > 9)
print(x > 10 or y < 5)
print(not(x < 5 and y < 10))
```
Output:
```
True
True
True
```
*Identity and membership*: Identity operators return true if a both values are the same and false if otherwise. They include: is, is not. Membership operators return true is an object is present in another and false if otherwise. They include: in, not in.
```python
x = 20
y = 10

#identity
print(x is y)
print(x is not y)

#membership
print("ment" in "mentality")
print("mint" not in "mentality")
```
Output:
```
False
True
True
True
```
####Python statements and loops
**If...else and if...elif statements**
You want to create a Python program that does one thing if input is true and does another if input is false; that is where you implement the if...else and if...elif statements.

* If...else statement:
```python
age = int(input("How old are you: "))

if age < 13:
    print("You are not allowed to play this game!")
else:
    print("You can proceed to the arena.")
```
Output:
`Output depends with the input given by the user. If age 
is greater than 13, you shall proceed but if otherwise, you will not be allowed to play the game.`

* If...elif statement:
```python
marks = int(input("Enter your score: "))

if marks >= 70:
    print("Pass")
elif marks >= 50:
    print("Average")
else:
    print("Good try! You can do better next time!")
``` 
Output:
`If marks are 70 and above, output will be "Pass. If they are from 50 to 69, output will be "Average while if 49 and lower, output becomes "Good try!..."`

**Python Loops**
1. For Loop

For loop in Python is used for iterating over a sequence.
```python
students = {"Zelda", "John", "Jane", "Simon"}
for student in students:
    print(student)
```
Output:
```
Simon
John
Zelda
Jane
```
2. While Loop

While loop executes a set of statements as long as the condition is true or satisfied. While loops can execute to infinity so it is important to use increments or *break* statement.
```python
n = 10
count = 0
while count < n:
    count += 1 #increment added so loop can break after reaching n
    print(count)
```
Output:
```
1
2
3
4
5
6
7
8
9
10
```

####Python Data structures
* Lists

Lists, commonly known as arrays, are data structures with changeable sequences of elements. Elements inside a list are called items. They are defined by square brackets - [ ].
```python
market = ["books", "groceries", "bread", "electronics"]
print(market)
```
Output:
```python
['books', 'groceries', 'bread', 'electronics']
```
* Tuples
Tuple is a data structure that stores an ordered sequence of elements. They cannot be changed once or modified like lists.
They are described by parentheses - ( ).
```python
market = ("books", "groceries", "bread", "electronics")
print(market)
```
Output:
```python
("books", "groceries", "bread", "electronics")
```
* Sets
Set is a data structure used to store multiple elements in a single variable. They are unindexed, unordered and cannot be changed. They are described by curly brackets - {}.
```python
students = {"Zelda", "John", "Jane", "Simon"}
print(students)
```
Output:
```python
{"Zelda", "John", "Jane", "Simon"}
#note: order of elements might change as sets are unordered.
```
* Dictionaries
Dictionary is a data structure that stores elements in a key:value pairs. They are also known as associative arrays and described by curly brackets - {}.
```python
students = {1: "Zelda", 2: "John", 3: "Jane", 4: "Simon"}
print(students)
```
Output: 
```python
{1: "Zelda", 2: "John", 3: "Jane", 4: "Simon"}
```

####Python user-defined functions
A python user can declare functions using the *def* keyword and thereafter, give their function a name, include arguments if any present inside parentheses and define what the function would do.
Even without arguments, always include parentheses for environment to identify that a function is being declared.
```python
#declaring the function
def powers(y, x):
    n = y**x
    return n

#call the function and give arguments to get output 
powers(2, 2)
```
Output:
```
4
```

####Python Classes
Python is an object-oriented programming language and thus, the introduction of classes and objects.
* self parameter is used to access variables used in a class
* __init__() function is used to assign values to object properties.
```python
class person:         #declaring a class that takes user name and age
    def __init__(self, name, age): #initializing properties
        self.age = age
        self.name = name

p = person("John", 21) #declaring an object

print(p.name, p.age)
```
Output: 
```
John 21
```

##Conclusion:
This is just the beginning. Python is a very interesting programming language that you can use to develop your skills in programming and become a pioneer in your desired field of work. Happy Coding and all the best as you begin your Python journey!