Python: Basic Questions – Functions & If Statements

Question 1: Using Functions
Using a function to allow a “block of code” to take in arguments/values and returns a result.
Task: Print 2*(n+1) to m using the double and increment functions provided.
‘2’ -> ‘6’
‘0’ -> ‘2’
‘-3’ -> ‘-4’

When given the functions:
def increment(x):
Increment the argument (ie, return the next largest integer).
x (int): The number to increment.
The next largest integer after the argument.
return x + 1
def double(x):
Double the argument.
x (int): The number to double.
Double the argument.
return 2*x

Answer (Placed after the functions):

Explanation: The functions given, show the values required to be inputted in, for it to return a result. In order to achieve our aim, we need to make the inputted string into an integer, and assign it to a variable. After this, we use the functions, and we first use the increment function and then the double function. Both these functions required one input value , and returns one output value. This is assigned to another variable

Question 2: If Statements
If statements allow you to run different code based on conditions that you set.
If condition evaluates to True, code (which could be many lines of statements) will be run.
If condition evaluates to False, then Python will skip to the next line of code after the if statement.
Task: Use an if statement to print the absolute value of a number.


Question 3: If Statements Using Else
In the previous question, we used an if statement to run code when a condition was True. Often, we want to do something if the condition is False as well. We can achieve this using else. Here, if condition evaluates to False, then Python will run the block of code under the else statement.
Task: Print either ‘vowel’ or ‘consonant’.
‘a’ -> ‘vowel’
‘b’ -> ‘consonant’

When given the function:
def is_vowel(c):
return c == ‘a’ or c == ‘e’ or c == ‘i’ or c == ‘o’ or c == ‘u’
# The above definition uses or, below is an alternative definition that uses in
# def is_vowel(c):
#     return c in ‘aeiou’


Explanation: Any line of code under the first part of the if, else statement (the if … part) will be run when the condition is satisfied. The second part of the if, else statement (else:) will be run only when the condition is not satisfied.

Question 4: If Statements Using Elif and Else
In the previous question, we used an if statement that contained an else branch. Sometimes, however, we have more than one condition we wish to test. This could be done using multiple if statements but can often also be accomplished using the if-elif-else form:
Task: Print ‘negative’, ‘zero’, or ‘positive’
‘-6’ -> ‘negative’
‘0’ -> ‘zero’
‘4’ -> ‘positive’


Explanation: Same explanation as before, but an extra condition is added by using elif, and if the condition after elif: is satisfied, the line of code under the elif: will be run.

, , , , , , , , , , , , , ,

No comments yet.

Leave a Reply

Powered by WordPress. Designed by WooThemes

Free WordPress Themes