{ "cells": [ { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# Practice Problem 1\n", "# Write a function that returns the maximum of two numbers.\n", "#\n", "def max2(num1,num2): # function header or function signature; contains NAME of function and a list of parameter NAMES \n", " if num1 > num2:\n", " return num1\n", " else:\n", " return num2\n", "\n", "# What is a parameter?\n", "# it is a generic name to variable that is typically an INPUT to a function " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40\n", "35\n" ] } ], "source": [ "print(max2(10,20))\n", "print(max2(30,5))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "220" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max2(100,200)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def nationality(name):\n", " ???\n", " ???\n", "\n", "\n", "def uselessFunction(name):\n", " if nationality(name) == \"India\":\n", " return \"Namaste\"\n", " else:\n", " return \"Hello\"" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uselessFunction(\"\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Namaste'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uselessFunction(\"James\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n", "Namaste\n" ] } ], "source": [ "for i in range(10):\n", " print(uselessFunction())" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Namaste\n" ] } ], "source": [ "print(\"Namaste\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def square(n):\n", " return n*n" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "square(10000000000000000000000000000) == 100000000000000000000000000000000000000000000000000000000" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# FINITE DIGITAL COMPUTER\n", "# ANALOG COMPUTER\n", "# INIFINITE PRECISION ARITHMETIC" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "# Problem 2\n", "# Write a function called fizz_buzz that takes a number as parameter. \n", "# If the number is divisible by 3, it should return “Fizz”. C1 \n", "# If it is divisible by 5, it should return “Buzz”. C2\n", "# If it is divisible by both 3 and 5, it should return “FizzBuzz”. C3\n", "# Otherwise, it should return the same number.\n", "\n", "# C3 implies C1 and C2\n", "def fizz_buzz(num):\n", " if num%3 == 0 and num%5 == 0:\n", " return \"FizzBuzz\"\n", " elif num%3 == 0:\n", " return \"Fizz\"\n", " elif num%5 == 0:\n", " return \"Buzz\"\n", " else:\n", " return str(num)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'8'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fizz_buzz(8)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9%3" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "13//3\n", "13%3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "+, -, *, /, %, //, " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }