1. The document discusses functions in Python, including how to create them using the def keyword and pass arguments. Functions allow reusable blocks of code to be executed repeatedly without rewriting.
2. A function is defined using def followed by the function name and parentheses. Code is indented inside the function. Functions can optionally return a value using the return keyword.
3. The document provides examples of creating simple functions that print or return values, and how to call functions by name and pass arguments. Functions are an important part of effective Python programming.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
32 views
06-Control Flow and Functions
1. The document discusses functions in Python, including how to create them using the def keyword and pass arguments. Functions allow reusable blocks of code to be executed repeatedly without rewriting.
2. A function is defined using def followed by the function name and parentheses. Code is indented inside the function. Functions can optionally return a value using the return keyword.
3. The document provides examples of creating simple functions that print or return values, and how to call functions by name and pass arguments. Functions are an important part of effective Python programming.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16
Foundation Course in
Python Loops:
FOR LOOP For Loop: A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once
for each item in a list, tuple, set etc. Range () Function: The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. User Input:
Input (_) Function
Functions Functions: ● Creating clean repeatable code is a key part of becoming an effective programmer. ● Functions allow us to create blocks of code that can be easily executed many times, without needing to constantly rewrite the entire block of code. Functions: ● Functions will be a huge leap forward in your capabilities as a Python programmer. Functions: ● It is very important to get practice combining everything you’ve learned so far (control flow, loops, etc.) with functions to become an effective programmer. How to create Function: def name_of_function():
Keyword telling Python
this is a function. A colon indicates an upcoming indented block. Everything indented is then “inside” the function How to create Function: def name_of_function(): ’’’ Docstring explains function. ’’’
Optional: Multi-line string to describe
function. How to create Function: def name_of_function(): ’’’ Docstring explains function. ’’’ print(“Hello”) Code then goes inside the function. How to create Function: def name_of_function(): ’’’ Docstring explains function. ’’’ print(“Hello”) >> name_of_function() Function can then be >> Hello executed/called to see the result. How to create Function: def name_of_function(name): ’’’ Docstring explains function. ’’’ print(“Hello ”+name) >> name_of_function(“Jose”) >> Hello Jose Functions can accept arguments to be passed by the user. How to create Function: ● Typically we use the return keyword to send back the result of the function, instead of just printing it out. ● return allows us to assign the output of the function to a new variable. How to create Function: def add_function(num1,num2): return num1+num2
>> result = add_function(1,2) Return allows to save the
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More