Python Basics! Python 101!
Tags: python, programming, beginners, machinelearning
Published: 2021-07-24 04:47:02

Python is an incredibly versatile, expansive language which due to its similarity to every day languages, is surprisingly easy to learn among inexperienced programmers. Python is very popular among professionals and students because it has a clear and simple syntax that will get you writing useful programs in short code. It even has an interactive mode, which offers immediate feedback, allowing you to test out new ideas almost instantly and has plenty of libraries allowing programmers to get a lot done with relatively little code.<br>
<H2>Some of python's applications include:</H2>
<ul>
	<li>Python is used in most of the search engines</li>
	<li>Enables banks to track the money in their accounts</li>
	<li>Used to program machines that are used to perform tricky medical procedures in hospitals.</li>
	<li>Desktop applications</li>
	<li>Web development</li>
	<li>Automation</li>

</ul>

<h3> Getting started with python</h3>
<p>As you can see python is quite handy now let’s get started.
To install windows on your system visit  the python website.
<a href ="htttps://www.python.org">Here</a> and click on the latest version of Python for Windows. The installer file will download automatically. Of the different installer options, select “executable installer”. Double-click the installer file to install Python. Choose “install for all users” and click “next” at each prompt, without changing the default settings.<br>
When the installation is finished, check that it was successful by opening the IDLE program. Go to the “Start” menu, choose “All Apps”, and then select “IDLE”.</p>
<h4>On a mac</h4>
<p>
Go to the Python website.<a href ="htttps://www.python.org">Here</a> and click on “Downloads” to open the download page.From the downloads options, click on the latest version of Python 3 that matches your operating system. 
The Python.pkg file will download to your Mac automatically.
Install python. You’ll find the .pkg file in the “Downloads” folder. Its icon looks like an opened parcel.Double-click it to start the installation. At the prompts, click “Continue” and then “Install” to accept the default settings.
Open IDLE. When the installation is finished, check that it was successful by opening the IDLE program. Open the “Applications” folder, and then the “Python” folder. Double-click “IDLE”
</p>
<h3>Your first program in python</h3>
<p>This is now the time to get our hands dirty and what better way than the programmers best friend “hello world”.<br>
First create a new folder where you will be saving your python files, python you wouldn’t want your files flying about the home directory. Then, launch the vs code and locate your new folder. 
In Vscode install the python extension and then create a file called hello_world.py, enter the following command and then save.
```py
Print(‘hello World’)
```
> Print () is an in-built python function that displays a message on the screen. In our example it will output ‘hello World’

<h3>Executing the python program</h3>
To execute our program click the run button and the output will be displayed in the terminal. The message that will be seen on the screen is ```py
hello World
```
Note that you are more or less familiar with python lets dive into the basic syntax

<h3>Python basic syntax</h3>
Keywords are words that python reserves for defining the syntax and structure of the python language. Hence, you cannot use them as a regular identifier when naming variables, functions, objects, classes, and similar items or processes. They include but not limited to:
<table style= "border: 1px solid black">
 <tr>
    <td>and</td>
    <td>assert</td>
    <td>break</td>
    <td>for</td>
    <td>not</td>
    <td>is</td>
  </tr>
  <tr>
    <td>lambda</td>
    <td>else</td>
    <td>from</td>
    <td>finally</td>
    <td>pass</td>
    <td>for</td>
  </tr>
  <tr>
    <td>raise </td>
    <td>None</td>
    <td>False</td>
    <td>del</td>
    <td>if</td>
    <td>import</td>
</tr>

</table>
> All the keywords except True, False and None are in lowercase and keywords are case sensitive.
> An identifier is a name given to a variable, class, function, string, list, dictionary, module, and other objects.<br>

<h3> Rules for writing identifiers</h3>
1.	Identifiers can be a combination of letters in lowercase or uppercase or digits and an underscore. E.g  p, P lowercase, UPPERCASE, python_basics1
2.	An identifier cannot start with a digit.
3.	Key words cannot be used as identifiers.
4.	An identifier cannot contain special characters e.g  ‘#@$%
5.	An identifier can be of any length.

<h3>Naming Global Variables</h3>
Global variable names should be written in lowercase. Names consisting of two or more words should be separated by an underscore.
<h3>Naming Classes</h3>	
The CamelCase convention is used when naming a class.
<h3>Naming instance Variables</h3>
Instance variables consisting of several words should be separated by an underscore and variable names should be in lowercase	
<h3>Naming modules and Package Names</h3>
Names for modules should be in lower case, they should be short one-word
<h3>Naming functions</h3>
Function names should be in lowercase and those with several words should be separated by an underscore	
<h3>Naming Constants</h3>
Constants are written in all uppercase letters and underscores are used to separate those with several words	
<h3>Comments</h3>
Comments are notes used to explain the code in the program and cannot be seen by the user running the program but can be seen in the source code. Comments make it easy for you and other programmers to evaluate your work many years after the code was written.
```py
#single line comment
'''
multiline comment
using a doctstring
'''
```
<h3>Variables and Data Types in python</h3>
<h4>Variables</h4>
A variable is a reserved memory location that can be used to store values. This means that when you create a variable you reserve some space in the memory. You can  declare a variable using an appropriate name and assigning a variable to it using the assignment operator (=)
```py
student ="John smith"
age = 20
amount = 20.05
paid = False
```
> You don’t   need to tell python that a variable should contain a specific type of data. Python automatically identifies the data type based on the values you assign to the variable.<br>

<h4> Data types</h4>
**1). strings**
  A string is an ordered series of Unicode characters which may be a combination of one or more letters, numbers, and special characters. It us an immutable data  type which ,means that when its created it cannot be changed.

```py
single_string = 'this is a single string'

multiline_string ='''
                    like
                   this
                   example
                   '''

#for escaping characters
esc_string = "The teacher said:\ "You are a genuis.\""
print(esc_string) # The teacher said: "You are a genuis"

#concatinate stringg
greet ="hello"
name ="john"
print(greet+ ""+ name)# hello john

#interpolation
# method 1
name = "John"
age ="20"
message= f"{name} is {age} years."

#method 2
name = "John"
age ="20"
message = '{}is {}years.formart(name,age)'

#method 3
name = "John"
age ="20"
message = '{} is {}.format(first=name, last=age)'

#accessing characters in a string
name = "John Smith"
print(name[0])#J
print(name[6:9])#Smith
```
**2.) Number Data types**
There are 3 numeric data types in python i.e int, float and complex
```py
normal_integers = 46
floating_point_numbers = 50.04
complex_numbers = 3 +5i

# converting from one numerical Type to Another
float(56)
int(3.0004)
complex(45)
```
<h3>Arithmetic Operators</h3>
```py
print(3+2) # 5 (addition)
print(10-5) # 5 (subtraction)
print (3*6) # 18 (multiplication)
print(46/2) # 23 (division)
print(3**3) # 9 (exponent)
print(11%4) # 3 (modulus)
print(60//4) # 15 (floor division)
```
**3). Booleans**
```py
d= True
b= False
```
**4). Lists**
A list is one of the most commonly used sequences and it is the most flexible type. It can hold one data type or a combination of several data types. A list is mutable that is you can add, remove or modify its elements.
```py
# can create a an empty list with this syntax 
my_list =[]

# lists with integers
num_list = [1,2,3,4,5]

# list with strings
string-list = ["John", "Mary", "Peter"]

# list with mixed data types
mixed_list = ["John", 24, True]

# methods on lists
children = ["John", "Mary", "Sophy", "Peter"]
# append
children.append("Jerry")

# insert
children.insert(2,"Tom")

# remove
children.removed("Sophy")

# get length
print(len(children)) # 5

```
**5). Tuples**
Tuples are just the reverse of lists. They are immutable and surrounded by ().
```py
new_tuple  = ("a","b","c","d","e")
print(len(new_tuple)) # 5
print(new_tuple[3]) # d
```
**6). Dictionaries**
This is unordered collection of key-value pairs which are separated by a colon **,** and enclosed in curly braces **{}**. A dictionary can contain any data type and is mutable however its keys are immutable and can only contain numbers, tuples and  strings
```py
ages= {
"John": 20,
"Mary":21,
"Tom":25,
"Jerry":19
}

# access, modify, delete

print(ages[Mary]) # 20
print(len(ages))  #  4
```

This marks the end of our introduction, I hope by now you know what python is why choose python, applications of python and how to install it on your machine. We have gone through how to save python files and execute them, the basic syntax in python and the various data types. Thank you for reading