Python 101: The Ultimate Python Tutorial For Beginners
Published: 2022-04-22 12:30:08

#Python Fundamentals 1
Programming as a concept is one of the most fascinating concepts in computer science. The concept of being able to make a program that can ease your workload by simply writing a script that automates your work. One of the most fascinating languages in programming in python and bash. Both languages are used in scripting; Python is also used for software development, web development, and data analytics among many other applications. Python is an object-oriented programming language it's slightly similar to Java, another programming language.
Python was invented in the late *1980s* by **Guido van Rossum** it was created to simplify the already complex programming languages that existed before. Python's name is derived from the British comedy group Monty Python, whom Python creator Guido van Rossum enjoyed while developing the language. Python has developed a lot from the first time it was created, recently python3 came into play after some years of using python2, and there were changes to some of the functions in python2 and some improvements.
##Why Should I Study Python?
Most people ask why I should study python as the first programming language. The answer to that question might be because python is a cross-platform meaning that it can be used in different devices and operating systems such as Windows, Linux, Mac, and Raspberry Pi just to mention a few. Secondly is because python has a simple syntax that allows the programmer to write his or her programs with fewer lines than some other programming languages an example will be if you wanted to write a program that says _hello world_ it would only take one line 

```python
print("Hello World!" 
```
unlike other programming languages that may take more than three lines. Another reason is that python runs on an interpreter system, meaning that the written code can be executed as soon as it's written. This means prototyping can be very quick.
Python is very strict with indentation; indentation refers to the space at the beginning of a code line. When you do not indent your code after using a conditional statement python will always throw errors since it does not understand the code.
##Variable in Python
Variables are memory locations in programming they hold values that one declares in their code such as 

```python
age = int(10)
```
 In this example, we have created a memory location and named its age and we have set the memory location to store integer type of data only, and then we have stored the value 10 into our *memory location*. Variables do not need to be declared with any particular type, and they can even change type after they have been set. Variables do not only take integer data types, you can also declare a variable with **string values, characters, floats, doubles and Boolean**, etc.
Variables are case sensitive thus 

```python
a = 5 
```
is different from 

```python
A=5
```
 thus one should be careful when declaring his or her variables. String variables can be declared with single or double quotes i.e 

```python
name = " Python"
```
 is the same as 

```python
name='Python'
```

Variables have their own rules also known as the conventions of declaring variables, they include: 
- A variable name must begin with a letter or the underscore character
- A variable cannot begin with a number
- A variable can only contain alphanumeric characters and underscores(A-z,0-9, and _)
- Variable names are case-sensitive (bot, Bot, and BOT are three different variables)

###Output Variables
Output variables: in python, the `print()`functions are usually used to output variables an example is:

```python
X = "what's your name?"
Print(x)
```
If the above example is compiled the output will be: <u>**what's your name?**</u> One can also print multiple variables by separating them with commas an example can be; `print(x,y,z)`. Apart from outputting multiple variables, one can also concatenate variables together to form a sentence. An example of concatenating variables:

```python
 x = "Hey "
 y = "am "
 z = "Python"
print(x+y+z)
print(x,y,z)
```
**output: Hey am Python**
**NB:**  You cannot concatenate a string with integer values in case you do it you will be met with a _TypeError: unsupported operand type(s) for +: 'int' and 'str'_
The best practice for outputting multiple variables in the `print()` function is to separate them with commas, which even support different data types:
###Global variables
Global variables: this is a variable that is declared outside a function. Global variables can be used by anyone, both inside of functions and outside.
In a normal case when you create a variable within a function, that variable is usually local, and can only be used within that function. To create a global variable inside a function, one can use the _global keyword_. Also, the global keyword can be used inside a function if you want to change a global variable inside a function.

```python
# Creating a variable outside a function and using it within the function
opinion = "Python is amazing"

def myopinion():
  print(f"How is Python:{opinion}")

myopinion()
```

```python
# creating a global variable inside a function
def greetings():
  global h
  h = "Hae 2"

greetings()

print(f"Hae:{h}")
```
###Comments
Comments are used in programming to give _more information_ about what a section of the code does; they are also used to make the code _more readable_. Comments are not executed when compiling your code since they are not part of the code so the compiler assumes the comments. Comments in Python start with `#`, anything is written after the _hash (#)_ python will ignore it since it's a comment.
<u>Example of how to use comments</u>

```python
#This is a comment
#The line below prints Hello World!
print("Hello World!)
```
Something to note is that comments are not only used for explaining the code, they can also be used in _preventing python from executing code._

```python
#print("Hello World")
```
The above code will be ignored by python since it has been commented out.
The `#` can only be used in commenting single line if you want to comment on multiple lines you will have to use multi-line comments. Since python does not have syntax for multi-line comments we use _docstrings_ to achieve multi-line commenting, this is because python ignores string literals that are not assigned to a variable. You can achieve this by using triple double quotes ("the lines of code you want to comment go here "") or single quotes three times.
<u>Example</u>

```python
""""
This is a program 
That is written in 
More than a single line
"""
print("Hello Python") 
```