Git Product home page Git Product logo

u4_lesson_python_functions's Introduction

Python Functions

Overview

In this lesson, we'll be learning about python function syntax, how it uses indentation to separate blocks of code, return statements and arguments, along with how it compares with JavaScript function syntax.

Objectives

  • Learn Python function syntax
  • Learn proper indentation for code blocks
  • Practice returning values from functions
  • Practice passing arguments/parameters to functions

Getting Started

  • Fork and clone this repository and cd into the directory

Function Syntax

In javascript we declare functions like this:

const myFunc = () => {
  return 'Hi'
}

Let's see how we declare functions in python:

def my_func():
  return 'Hi'

As you can see, we first define a function using the def keyword, we set the name of the function as my_func and add a set of parentheses, (), notice the : symbol. In python we use the : to define a block of code.

One important thing to note here, Python is very strict on indentation, you can either use 1 tab or 2 spaces to indent your code. Indentation is how python read's blocks of code.

  • Note: If you do not indent your code, it will absolutely break and not compile.

Function Arguments/Parameters

We can also pass in arguments and define parameters in Python functions by creating a variable in the parentheses, example:

def sum_func(num):
  return num

num works as a placeholder to let our function know that it will receive something to perform a calculation on when it is invoked.

To invoke functions, it follows the same pattern as most programming languages, Example:

  • JavaScript
    const myFunc = () => {
      console.log('Hi)
    }
    myFunc()
    // Prints Hi To The Console
  • Python
    def myFunc():
      print('Hi')
    
    myFunc()
    # Prints Hi to the console

You Do: Putting Functions To Use

Now that we've seen how python functions work, let's get a little practice using them!

Follow the directions in main.py to complete this lab.

Test your code using python3 func_test.py , you should have 5 passing tests. Happy Coding!


Recap

A few things worth reiterating with python functions:

  • A colon : is used to denote the end of a function declaration
  • Every block of code must be indented
  • def is the keyword to declare a function in python
  • Arguments are passed into the parenthesis after the function_name()

Resources

u4_lesson_python_functions's People

Contributors

ahonore42 avatar anpato avatar nobodyslackey avatar gonczarm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.