Algorithms with Python Variables and Conditions
Published: 2021-09-07 00:44:04

### Continuation...  
[Previous lesson, Introduction to Computer Programming for Coders](https://dev.to/fabaladibbasey/introduction-to-computer-programming-for-coders-1ga6) 

Since we don't have to directly deal with `0s and 1s` anymore, thanks to Programming languages. We are going start off by installing programming language, python into our computer and setting up the environment for writing and executing codes.

**Downloading and setting up**

> Setting up is one of the hardest challenge you'll face as a newbie, new to coding! So don't give up. It's not hard but everything is just new to you

Head over to  [Python.org website](https://www.python.org/downloads/) and download the latest version of python. If you're having trouble installing Python, refer to some random YouTube video for help. Oh right once we install python, we need some editor just like we need Microsoft word for working with texts and excel for operations on Data.

Once we get all of them up and running let's now instruct our computer to greet the user.
```py
user_name = input("Enter your name: ")
print("Hello", user_name + '!')
``` 
Congratulation! You've written your first python program! 

What have we done?
We asked user for an input, bind user_name to the input value and finally we print out greeting which consist of the text "Hello" with Whatever user put in, in this case we're expecting user's name.
`user_name` here is **Variable**, `input` and `print` are **Function** and `=` is the **assignment operator**. Notice we enclose "Hello" and '!' into quote and that's exactly a way of generating texts (strings) in python. A lot of Grammars already but more on them later. 

**Challenge 1**:
Ask user for two input, name and surname, store them in a variable name and surname respectively and print out the full name.

>Hint:
You'll need two input function

**Solution**

```py
name = input("Enter your name: ")
surname = input("Enter your surname: ")
print("your full name is", name, surname)
                
``` 


Now let's ask the user for the year they were born and we'll tell them their age.

```py
birth_year = input("Which year where you born? ")
age = 2021 - int(birth_year)
print("You're", age, "years old")

``` 
Notice we pass the birth_year variable into `int function`. This is because the input function return string (one or more characters or texts to be precise) and all what `int function`does is to convert the string into an `Integer number`.

Challenge 2: 
Ask user for two integer numbers and return the sum of the numbers

>Hint:
- You'll need two input function
- Don't forget to cast/convert the inputs into integer number

**Solution**

```py
first_number = input("Enter an integer number: ")
second_number = input("Enter another integer number: ")
sum_of_inputs = int(first_number) + int(second_number)
print("Sum of the two numbers is", sum_of_inputs)

``` 

## Variables
We've already seen variables, `user_name, age etc...` in action. Can you tell what they do?

1. We use variable to map to our values for references purpose
1. Variables improve the readability of our code. As in `user_name`, whoever read the code will know exactly that the input value is user's name, and `sum_of_inputs` will tell the reader that the value is the result of summation of inputs
1. Re-usability of values
For instance, performing five basic operations with two variables

```py
num1 =  9
num2 = 2
print(num1 + num2)
print(num1 - num2)
print(num1 * num2)
print(num1 / num2)
print(num1 % num2)

``` 
At any point you can change num1 or num2 to any value and that we'll be reflected in all the print statement.
Hence the name variable, it value can be changed or re-bind to another value and operations on variables will still remain unaffected.
```py
num1 = 9
num1 = 7
print(num1)
#num1 is re-bind to value 7

```
`%`? It is called modulo or remainder operator.
In Python, `/` gives floating point value (decimal), `//` gives quotient and `%` give you the remainder of a division.

Let's explore some application of modulo operator in programming.
Get an integer from a user and tell whether the integer is an Even or Odd number.
Two questions we should ask ourselves before we start to approach the problem

1. How can we get an integer from a user?
1. How can we determine Even or Odd number?

Well, We can use input function to get data from user, cast/convert our data to an integer number using int function and all what we know about Even numbers is that they're divisible by two ( can be divide by two without a remainder ) and any number that's not an Even number must be an Odd number.

```py
int_number = int(input("Enter an integer number: "))
if (int_number % 2 == 0):
    print(int_number, "is an Even number")
else:
    print(int_number, "is an Odd number")
    
``` 
**New concept Alert!** Condition or Branching, if/else more on them later.

Back to **Variables**

Question: You're given two cups, orange and banana each contains a juice of it's name. orange cup contains orange juice and banana cup contains banana juice? Can you write a program that will swap the juices in these cups. When swapped orange cup should now contain banana juice and Banana cup should contain orange juice.
>Hint:
- Demonstrate it with two cups, orange and banana with their respective juices
- Try swapping the juices in the cups
- If still stuck look for additional container (variable)
- Now swap them

**Solution**
Will be posted later

## Condition/Branching
We've seen simple version of branching, when we check whether the user's input is `Even or Odd`. 
The syntax was, 
```py
if (<condition>):
    #some logic
else:
    #some other logic

```
We used `==` to check whether remainder is 0 when divided by 2. When `True` we know exactly it's `Even number` else it's `Odd number`
`==` is the comparison operator since `=` was reserved for  an assignment operator. 
Indentation is important, It define the block of codes. From the syntax above, `some logic` belong to the body of `if condition` while `some other logic` belong to `else scope`. More on scopes later.

**Comparison operators**
- `==` equal
- `!=` not equal
- `<` less than
- `<=` less than or equal to
- `>` greater than
- `>=` greater or equal to

Can we now determine whether our integer input is neutral ( 0 ), negative ( less than 0 ) or positive ( greater than 0 )?

>pseudo code
1. Ask user for an integer input
1. Cast/Convert the input into integer by passing it into `int function`
1. Check if input is equal to 0, print 'neutral' into the console
1. Else if input is less than 0, print 'negative' into the console
1. Else the input must be positive then print out 'positive' to the console

**Solution**
```py
int_number = int(input("Enter an Integer number: " ))

if ( int_number == 0 ):
    print('neutral')
elif( int_number < 0 ):
    print('negative')
else:
    print('positive')
                 
``` 
**Exercise**

Write a program that take an integer number from user and print `Fizz` when it's divisible by 3, print `Buzz` when it's divisible by 5 and print `FizzBuzz` when it's divisble by both 3 and 5.

I know you must be exhausted. See you in **Algorithms With Loops** 
Thanks for following the series.
Don't forget to connect with me on  [Twitter](https://twitter.com/DibbaseyFabala) and  [LinkedIn](https://www.linkedin.com/in/fabala-dibbasey-b69a27207/)