You are on page 1of 4

Boolean

----------
The (and) Operator's Truth Table

Expression Evaluates to...


True and True True
True and False False
False and True False
False and False False

The (or) Operator's Truth Table


Expression Evaluates to...
True or True True
True or False True
False or True True
False or False False

The (not) Operator's Truth Table


Expression Evaluates to...
not True False
not False True

Element of Flow Control


-------------------------------
Conditions
A flow control statement decides what to do based on whether its condition is True
or False, and almost every flow control statement uses a condition.

Blocks of Code
1. Blocks begin when the indentation increases.
2. Blocks can contain other blocks.
3. Blocks end when the indentation decreases to zero or to a containing block's
indentation.

Flow Control Statements


if Statements
The if keyword
A condition (that is, an expression that evaluates to True or False)
A colon
Starting on the next line, an indented block of code (called the if clause)

else Statements
The else keyword
A colon
Starting on the next line, an indented block of code (called the else clause)

elif Statements
The elif keyword
A condition (that is, an expression that evaluates to True or Flase)
A colon
Starting on the next line, an indented block of code (called the elif clause)

while Loop Statements


The while keyword
A condition (that is, an expressioin that evaluates to True or False)
A colon
Starting on the next line, an indented block of code (called the while clause)

break Statements
If the execution reaches a break statement, it immediately exits the while loop's
clause. In code, break statement simply contains the break keyword.
Pretty simple, right? Here's a program that does the same things as the previous
program, but it uses a break statement to escape the loop.

continue Statements
Like break statements, continue statements are used inside loops. When the program
execution reaches a continue statement, the program execution immediately jumps
back to the start of the loop and revaluates the loop's condition. (This is also
what happens when the execution reaches the end of the loop.)

for loops and the range() Function


The for keyword
A variable name
The in keyword
A call to the range() method with up to three integers passed to it
A colon
Starting on the next line, an indented block of code (called the (for) caluse)

The Starting, Stopping, and Stepping Arguments to range()

The Starting
Some functions can be called with multiple arguments separated by a comma, and
range() is one of them. This lets you change the integer passed to range() to
follow any sequence of integers, including starting at a number other than zero.

The Stopping
The first argument wil be where the for loop's variable starts, and the second
argument will be up to, but not including, the number to stop at.

The Stepping
The step is the amount that the variable is increased by after each iteration.
You can even use a negative number for the step argument to make the for loop count
down instead of up.
Running a for loop to print i with range (5, -1, -1) should print from five down to
zero.

Importing Modules
All Python programs can call a basic set of functions called built-in functions,
including the print(), input(), len() functions you've seen before. (standard
library)
The (math) module has mathematics-related functions, the (ramdom) module has ramdom
number-related functions, and so on.

The import keyword


The name of the module
Optionally, more module names, as long as they are separated by commas

Functions
------------
Python provides several built-in functions like these, but you can also write your
own functions. A function is like a mini-program within a program.

def Statements with Parameters


When you call the print() or len() function, you pass in values, called arguments
in this context, by typing them between the parentheses.

Return Values and return Statements


The return keyword
The value or expression that the function should return

Keyword Arguments and print()


Most arguments are indentified by their position in the function call.
E.g. random.randint(1, 10) is different from random.randint(10, 1)
random.randint(1, 10) will return a random integer between 1 and 10 - the first
argument is the low end of the range and the second argument is the high end
(random.randint(10, 1) causes an error.

Local and Global Scope


Code in the global scope cannot use any local variables.
However, a local scope can access global variables.
Code in a function's local scope cannot use variables in any other local scope.
You can use the same name for different variablels if they are in different scopes.
That is, there can be a local variable named spam and a global variable also named
spam.

Local Variables Cannot Be Used in the Global Scope


When the program execution is in the global scope, no local scopes exist, so there
can't be any local variables.

Local Scopes Cannot Use Variables in Other Local Scopes


The upshot is that local variables in one function are completely separate from the
local variables in another function.

Local and Global Variables with the Same Name


To simplify your life, avoid using local variables that have the same name as a
global variable or another loacl variable.
Since the separate variables all have the same name, it can be confusing to keep
track of which one is being used at any given time. Cause we should avoid using the
same variable name in different scopes.

The global Statement


1. If a variable is being used in the global scope (that is, outside of all
functions), then it is always a global variable.
2. It there is a global statement for that variable in a function, it is a global
variable.
3. Otherwise, if the variable is used in an assignment statement in the function,
it is a local variable.
4. But if the variable is not used in an assignment statement, it is a global
variable.

Exception Handling
A ZeroDivisionError happens whenever you try to divide a number by zero. From the
line number given in the error message, you know that the return statement in
spam() is causing an error.
Errors can be handled with try and except statements.

Dictionaries and Structure Data


---------------------------------------
is provides a flexible way to access and organize data.

Dictionary Data Type


- is a collection of many values.
- unlike indexes for lists, indexes for dictionaries can use many different data
types, not just integers.
- indexes for dictionaries are called keys, and a key with its associated value is
called a key-value pair.
- is typed with braces,{}.s
- can use integer values as keys (but they do not start at 0 and can be any
number.)

The key(), value(), and items() Methods


-these data types(dict-keys, dict-values, dict-items) can be used in for loops.

You might also like