Chapter 3 - Glossary Flashcards
Function
Function is a named sequence of statements that
performs a computation. When you define a function, you specify the name and the
sequence of statements.
Functions that convert values from one type to another.
int()
str()
float
>>> int('32') 32 >>> float(32) 32.0 >>> str(32) '32
int can convert floating-point values to integers, but it doesn’t round off; it chops off
the fraction part:»> int(3.99999)
3
What is module ?
A module is a file that contains a collection of related functions.Before we can use the functions in a module, we have to import it with an import statement: >>> import math
How to use modules
The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.
How to get value of pie
import math
pie = math.pi
print(f”Value of Pie - {pie:,.2f}”)
Value of Pie - 3.14
How many indentations win function body
Four Spaces.
How to define a function ?
The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By con‐vention, indentation is always four spaces. The body can contain any number of state‐ments.
def printName(): # Function Header print("Sundy") # Function Body
Function Defination on Interactive Mode ?
If you type a function definition in interactive mode, the interpreter prints dots (…) to let you know that the definition isn’t complete:
»> def print_lyrics():
… print(“I’m a lumberjack, and I’m okay.”)
… print(“I sleep all night and I work all day.”)
…
To end the function, you have to enter an empty line.
What is created when a function is defined ()
Defining a function creates a function object, which has type function:
How to read a Program .
when you read a program, you don’t always want to read from top to
bottom. Sometimes it makes more sense if you follow the flow of execution.
What is traceback ?
This list of functions is called a traceback. It tells you what program file the error occurred in, and what line, and what functions were executing at the time. It also
shows the line of code that caused the error.
NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_18764/1435530308.py in 5 print(value) 6 printTwice(math.pi) ----> 7 print(value)
NameError: name ‘value’ is not defined
What happen ,when you call a function in interactive mode.
Python displays the result:
»> math.sqrt(5)
2.2360679774997898
What are Fruitful and Void Functions
Inbuit Functions,for lack of a better name, I call them fruitful functions.
Functions don’t return a value. They are called void func‐
tions.
What is none type value.
Void function returns None value
The value None is not the same as the string ‘None’. It is a special value that has its
own type:
»> print(type(None))