{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "fxLSN_nvYGu4" }, "source": [ "# 4. Python Functions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "N_YIIbFsSNcC" }, "outputs": [], "source": [ "def add_numbers(a, b):\n", " result = a + b\n", " return result" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "yeuSZvRZYXfo" }, "source": [ "## Now let's break down the syntax:\n", "\n", "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.\n", "\n", "2. `(a, b)`: These are the parameters (or input arguments) that the function accepts. The function add_numbers takes two parameters: a and b.\n", "\n", "3. `:`: The colon marks the beginning of the function body, which is a block of code that defines the behavior of the function.\n", "\n", "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.\n", "\n", "5. `result = a + b`: This line creates a new variable called result and assigns the sum of a and b to it.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XLj7CWpbSTXN", "outputId": "146dd290-6797-43bf-e307-9ecb734dfdb2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "sum_result = add_numbers(3, 5)\n", "print(sum_result)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "7gDOh-iwY6wj" }, "outputs": [], "source": [ "def concatenate_strings(string1, string2):\n", " result = string1 + \" \" + string2\n", " return result" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Y29BeR8gcSDy", "outputId": "8599319f-7c6e-4fd0-8eb4-e49afb2b5bed" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am a python developer\n" ] } ], "source": [ "a = 'I am'\n", "b = 'a python developer'\n", "\n", "c = concatenate_strings(a, b)\n", "\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "h3g0EYF1cvmB" }, "outputs": [], "source": [ "def reverse_string(input_string):\n", " reversed_string = input_string[::-1]\n", " return reversed_string" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "V7pL2POOc2NC", "outputId": "8cafbb57-f1d9-4993-900f-a2f83445896b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "repoleved nohtyp a ma I\n" ] } ], "source": [ "d = reverse_string(c)\n", "\n", "print(d)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "WLXfqf6pdlJB" }, "source": [ "## Title: Understanding Functions in Python\n", "\n", "### 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." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "MjEjFDjTdwiq" }, "source": [ "1. Write a function named multiply_numbers that takes two numbers as input arguments and returns their product.\n", "\n", "Example:\n", "\n", "```python\n", "def multiply_numbers(a, b):\n", " # Your code here\n", "\n", "# Test your function\n", "print(multiply_numbers(3, 4)) # Output: 12\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Rbl1hxWHc7gS" }, "outputs": [], "source": [] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "omjVi60rd8lu" }, "source": [ "2. 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.\n", "\n", "Example:\n", "\n", "```python\n", "def count_vowels(input_string):\n", " # Your code here\n", "\n", "# Test your function\n", "print(count_vowels(\"hello world\")) # Output: 3\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "H9rs6cFyeGDj" }, "outputs": [], "source": [] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "siw2-H-ieH9c" }, "source": [ "3. 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.\n", "\n", "Example:\n", "\n", "```python\n", "def is_palindrome(input_string):\n", " # Your code here\n", "\n", "# Test your function\n", "print(is_palindrome(\"racecar\")) # Output: True\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "NnBgO9fFeQXp" }, "outputs": [], "source": [] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "k8x87xEGeRKi" }, "source": [ "5. 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.\n", "\n", "Example:\n", "\n", "```python\n", "\n", "def convert_temperature(temperature, scale):\n", " # Your code here\n", "\n", "# Test your function\n", "print(convert_temperature(32, \"F\")) # Output: 0.0\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cfIfilK_eYnK" }, "outputs": [], "source": [] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "miLs7HsbeZbC" }, "source": [ "6. 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.\n", "\n", "Example:\n", "\n", "```python\n", "def calculate_area(length, width):\n", " # Your code here\n", "\n", "# Test your function\n", "print(calculate_area(4, 5)) # Output: 20\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "U2jG_uv0ej2a" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "include_colab_link": true, "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }