Algorithms with Python Variables and Conditions
Fabala Dibbasey
Posted on September 7, 2021
Continuation...
Previous lesson, Introduction to Computer Programming for Coders
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 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.
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
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.
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
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?
- We use variable to map to our values for references purpose
- 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, andsum_of_inputs
will tell the reader that the value is the result of summation of inputs - Re-usability of values For instance, performing five basic operations with two variables
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.
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
- How can we get an integer from a user?
- 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.
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,
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
- Ask user for an integer input
- Cast/Convert the input into integer by passing it into
int function
- Check if input is equal to 0, print 'neutral' into the console
- Else if input is less than 0, print 'negative' into the console
- Else the input must be positive then print out 'positive' to the console
Solution
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 and LinkedIn
Posted on September 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.