print()
, input()
, len()
, str()
, int()
, float()
, map()
A Lambda Expression is also known as anonymous function. It is a function with no name that is used one time. A Lambda Expression is commonly used with built-in functions such as map()
and filter()
Named function
def times_two(var):
return var*2
Lambda function equivalent
lambda var: var*2
seq = [1,2,3,4,5]
map(lambda num:num*2, seq)
list(map(lambda num:num*2, seq))
seq = [1,2,3,4,5]
list(filter(lambda num:num%2==0, seq))
You can import modules and get access to new functions. The modules that come with Python are called standard library.
import random, sys, os, math
import random
random.randint(1, 10)
# from random import *
# random.randint(1, 10)
The sys.exit()
function will immediately quit your program.
# import sys
# sys.exit()
You can also install third-party modules using the pip
tool. To install pip
tool, the key is to know where your python is installed (where pip.exe is located) and add the path to environment variables. then you can finally install python modules in command prompt
. If you have anaconda installed, use anaconda prompt
instead. For more information on installing third-party modules, click [here]
The pyperclip
third-party module allows you to copy and paste text for reading and writing text to the clipboard.
To install pyperclip
:
# At command prompt/ anaconda prompt
# pip install pyperclip
To use pyperclip
:
import pyperclip
pyperclip.copy('Some text')
pyperclip.paste()
Functions are like a mini-program inside your program. The main point of functions is to get rid of duplicate code (de-duplicating).
The def
statement defines a function. The parameters are the variables in between the function's parenthesis in the def
statement.
def functionName(param):
print(param)
You can also set default value for parameter
def my_func(param='default'):
print(param)
To invoke a function, add ()
after the function name:
my_func()
The return value is specified using the return
statement
def functionName(param):
return param + 1
Every function has a return value. If your function doesn't have a return
statement, the default return value is None
(meaning lack of value) e.g. the print()
function returns None.
An argument is the value passed in the function call. Keyword arguments to function are usually for optional arguments. The print()
function has keyword arguments end
and sep
.
The print()
function automatically add a keyword argument new line
to end of function.
print('Hello')
print('World')
However, you can change the Keyword argument by using end
print('Hello', end=' ')
print('World')
If you pass multiple strings arguments to print()
, it automatically add space between strings
print('cat', 'dog', 'mouse' )
You can override this by passing the sep
argument to print()
print('cat', 'dog', 'mouse', sep="ABC" )
Add a pair of triple double quotes """
and put you documentation string inside
def my_func(arg=2):
"""
Docstring goes here!
"""
return arg*5
You can view the docstring in Jupyter notebook by clicking the line and then press shift-tab
A scope can be thought of as an area of the source code, and as a container of variables.
The global scope is code OUTSIDE of all functions. Variables assigned here are global variables. The local scope is code INSIDE of a function. Variables assigned here are local variables.
spam = 42 # global variable
def eggs():
spam = 42 # local variable
print('Some code here')
Code in the global scope cannot use any local variable
def spam():
eggs = 99
spam()
print(eggs) # NameError: name 'eggs' is not defined
Code in local scope can access global variable
def spam():
print(eggs)
eggs = 42
spam() # 42
def spam():
global eggs
eggs = 'Hello'
print(eggs)
eggs = 42
spam() # 42
print(eggs) # 42
Code in a function's local scope cannot use variables in another local scope
def spam():
eggs = 99
bacon()
print(eggs)
def bacon():
ham = 101
eggs = 0
spam() # 99
If there's an assignment statement of a variable in a function, that is a local variable unless that variable been marked global with a global
statement
Assignment statement = Local variable No assignment statement = Global variable
def spam():
eggs = 'Hello'
print(eggs)
eggs = 42
spam() # Hello
print(eggs) # 42
Global statement
def spam():
# change global eggs var to 'Hello'
# Do not create local var eggs
global eggs
eggs = 'Hello'
print(eggs)
eggs = 42
spam() # Hello
print(eggs) # Hello
The purpose of scope is to isolate code so that the cause of bugs is limited to a particular area of a program.