Open In Colab

4. Python Functions#

def add_numbers(a, b):
    result = a + b
    return result

Now let’s break down the syntax:#

  1. def: This keyword is used to define a new function in Python. It’s followed by the name of the function, in this case, add_numbers.

  2. (a, b): These are the parameters (or input arguments) that the function accepts. The function add_numbers takes two parameters: a and b.

  3. :: The colon marks the beginning of the function body, which is a block of code that defines the behavior of the function.

  4. Indentation (4 spaces or a tab): Python uses indentation to separate blocks of code. All the lines within the function body should be indented.

  5. result = a + b: This line creates a new variable called result and assigns the sum of a and b to it.

  6. return: This keyword is used to specify the value that the function will return when it’s called. In this case, the function returns the value of the result variable.

sum_result = add_numbers(3, 5)
print(sum_result)
8
def concatenate_strings(string1, string2):
    result = string1 + " " + string2
    return result
a = 'I am'
b = 'a python developer'

c = concatenate_strings(a, b)

print(c)
I am a python developer
def reverse_string(input_string):
    reversed_string = input_string[::-1]
    return reversed_string
d = reverse_string(c)

print(d)
repoleved nohtyp a ma I

Title: Understanding Functions in Python#

Instructions: Complete the following tasks to practice creating and using functions in Python. Make sure to test your functions by calling them with various inputs.#

  1. Write a function named multiply_numbers that takes two numbers as input arguments and returns their product.

Example:

def multiply_numbers(a, b):
    # Your code here

# Test your function
print(multiply_numbers(3, 4))  # Output: 12

  1. Write a function named count_vowels that takes a string as an input argument and returns the number of vowels (a, e, i, o, u) in the string.

Example:

def count_vowels(input_string):
    # Your code here

# Test your function
print(count_vowels("hello world"))  # Output: 3

  1. Write a function named is_palindrome that takes a string as input and returns True if the string is a palindrome (reads the same forwards and backwards) and False otherwise.

Example:

def is_palindrome(input_string):
    # Your code here

# Test your function
print(is_palindrome("racecar"))  # Output: True
  1. Write a function named convert_temperature that takes two arguments: a temperature value and a string representing the input temperature scale (“C” for Celsius or “F” for Fahrenheit). The function should convert the temperature to the other scale and return the result.

Example:


def convert_temperature(temperature, scale):
    # Your code here

# Test your function
print(convert_temperature(32, "F"))  # Output: 0.0
  1. Write a function named calculate_area that takes two arguments: the length and width of a rectangle. The function should return the area of the rectangle.

Example:

def calculate_area(length, width):
    # Your code here

# Test your function
print(calculate_area(4, 5))  # Output: 20