Ultimate python tutorial for beginners
Published: 2022-04-22 17:57:15


### Basics of python programming language

**Input, comments, printing and Variable operators**

## Definition of terms
1. Python - Is a programming language that is used for data science.
2. Code - Is a set of instructions that a programmer writes so as to perform a particular task.

**1. Printing**

**Example 1**

```python
print("I have just started python classes")
```
By printing we mean outputting our code to the console. As we carry on our studies we will get to learn the keywords which have two brackets as print() are called functions.

<u>Practice challenge</u>

Let's print "I am turning 25 years old."

**2. Comments**
They are used by programmers and data scientists to explain what their codes means. Usually this comments cannot be translated by the computer.

**Example 1**
The code below will print ("Hello world !").

```python
 # print("Hello world !")
```
**Example 2**
lets uncomment the above code

```python
print("Hello world !")
```

<u>Practice challenge 1</u>
Print your home time

<u>Practice challenge 2</u>
a = I live with my aunt

**3. variables**
A variable is a container that we can use to store a value . This value can be a number, a character, a string, a list , a dictionary etc...

**Example**
So as to create a variable we use the equals sign; =

```python
x = 3
```
Because we have assigned the values 3 to x
We now need to print out and see whether x has that value.
Then, print out x below this line using the print() function below.

```python
print(x)
```
<u>Practice challenge</u>
Assign Guardian angel to the variable gospel music artists.

**4. Operators**

**Example**
We are going to use the variables a and b to perform some arithmetic operations. Let's run this cell once then.

```python
print(a + b)
# print(a - b)
# print(a/b)
```
Uncomment the lines above to see the effect for multiplication, division and subtraction.

<u>Practice challenge 1</u>

```python
print(a - b)
```
```python
print(a * b)

```
```python
print(a/b)

```
<u>Practice challenge 2</u>

Get the sum of two numbers from a user's input

<u>Practice challenge 3</u>

While paying for her parking fee of kshs 50, Jane inserts a note into the paying machine and gets back her change. Write a program that reads as input the note that is greater than kshs.200. and returns a change in notes of 100 and 50 . Use the knowledge of variables, comments and operators and input print the number of denominations given.
**# hint**

```python
def no_notes(a);
q={500,200,100,50}
for i in a range (4);
q =q(i)
x + = int(a/q)
a = int(a%q)
if a>0;
x =-1
print(notes 100)
print(notes 50)
```

### Python conditional (IF) statements

For one to write useful python programs, we always need to check the conditions and apply a certain operation accordingly. Conditional statements like the if statement provide us with that ability.

**Example 1**
We can write an if statement by using the if keyword as shown.

```python
x = 300
y = 500

if y > x
print("y is greater than x")

```
**Example 2**
The elf keyword is a keyword that will try another condition if the previous condition was not true.

```python
x = 33
y = 33

if y < x:
print("y is less than x")
elf x == y
print("x and y are equal")
```
<u>Practice challenge</u>

Write a program that reads an input from a user . If the user enters a , e, i , o, u then our program should display a message indicating that the entered letter is a vowel.

**Example 3**
The else keyword will catch anything else which isn't caught by the preceding conditions.
First, we declare variables.

```python
if y > x:
print ("y is greater than x")
elf x==y:
print ("x and y are equal")
else
print("x is greater than y")

```
<u>Practice challenge 1</u>

Write a program that reads a wavelength from the user and it's color. Display an appropriate error message if the wavelength entered by the user is outside of the visible spectrum. You should use **else** keyword.

<u>Practice challenge 2</u>

Let's create a program that reads the name of a month from the user as a string, then displays the number of days in that month. The length of a month needs to also display 28 to 31 days. The program needs to also display 28 to 29 days for February. So that leap years are taken into account . The program should at least use the **else** keyword.

<u>Practice challenge 3</u>
Given the 3 sides of triangle x ,y and z. Find out whether the triangle is equilateral, isosceles or obtuse. N
Note, An equilateral triangle has all sides equal, isosceles means that two of the sides are equal not the third one, obtuse means all 3 are different . The user will be prompted to provide the values of x, y and z. The program should use **AND** keyword.

**An example on OR variable**

First we declare our variables

```python
x = int (input("Enter the values of x ="))
y = int (input("Enter the values of y ="))

if ( x>=15) or (y<=25)
print ("x >= 15 or y <= 25 so if statement is True")

else:
print(" values of x < 15 and y > 25 so if statements is False !")
```
Continue......