Automate the Boring Stuff with Python chapters 1 and 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

4 rules for variable names

1. It can be only one word. 2. It can use only letters, numbers, and the underscore(_) character. 3. It can't begin with a number. 4. Variables are case sensitive.

comparison operators and boolean operators

< > == can be used to return a boolean value. You can use and, or, and not to compare and return a boolean value.

Not operator

A "for" loop will loop a specific number of times. The range() function can be called with one, two, or three arguments. The break and continue statements can be used in for loops just like they're used in while loops.

data type

A data type is a category for values, and every value belongs to exactly one data type. Integers, floats, and strings are examples of data types.

Function

A parametized sequence of statements. Python provides built-in functions like print(), etc. but we can also create your own functions.

Return Statement

A return statement is used to end the execution of the function call and "returns" the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. Imporant if the return statement isn't put into your code it will not return the value to the function. So if you were doing something like a while loop it would only run once.

interactive shell

A shell is a program that lets you type instructions into the computer, much like the Terminal or Command Prompt on OS X and Windows, respectively.

elif

An elif (that is, "else if") statement can follow an if statement. Its block executes if its condition is True and all the previous conditions have been False. Order is important.

precedence

Another term for order of operations

print function values

Can be integers or strings. As along as they are all the same. Need to convert to concatenate if the values entered are different data types.

Conditions

Conditions always evaluate down to a Boolean value, True or False. conditions are basically the same thing as an expression. Except that condition is a more specific term to flow control and Booleans. All conditions are expressions, but not all expressions are conditions.

iteration

Each time the code runs in a loop.

expression

Expressions consist of values(such as 2) and operators (such as +), and they can always evaluate (that is, reduce) down to a single value.

break statement

In code, a break statement simply contains the break keyword. You can put a break statement under a line of code like an if statement and if the line evaluates as true then you break out of the loop.

if statement

In plain English, an if statement could be read as, "If this condition is true, execute the code in the clause."

function

Like a mini program in the program. For example print() is a function.

else

Optionally, you can have an else statement after the last elif statement. In that case, it is guaranteed that at least one (and only one) of the clauses will be executed.

boolean order of operations

Python evaluates the not operators first, then the and operators, and then the or operators.

Error: Can't convert 'int' object to str implicitly

Python thought you were trying to concatenate an integer to the string. Your code will have to explicitly convert the integer to a string, because Python cannot do this automatically.

File Editor

Similar to Notepad but has some specific features for typing in source code. The file editor can handle and save programs. Whereas the interactive shell can only do one line at a time.

commenting out code

Sometimes, programmers will put a # in front of a line of code to temporarily remove it while testing a program.

order of operations

The ** operator is evaluated first; the *, /, //, and % operators are evaluated next, from left to right; and the + and - operators are evaluated last (also from left to right). You can use parentheses to override the usual precedence if you need to. (PEMDAS)

The And operator truth table

The and operator compares two expressions and evaluates down to either true or false. For the And operator to evaluate as true both statements must be true, or else it is false.

break statement

The break statement will move the execution outside and just after a loop.

While Loop Statements

The code in a while clause will be executed as long as the while statement's condition is True. But at the end of a while clause, the program execution jumps back to the start of the while statement. The while clause is often called the while loop or just the loop.

continue statement

The continue statement will move the execution to the start of the loop.

range function

The first argument will 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 step is the amount that the variable is increased by after each iteration. range(0,10,2)would start at 0 go to but not include ten by increments of 2.

The Or operator truth table

The or truth table is comparing two expressions and if either of the expressions are true then it evaluates to a boolean value of true. Both have to be false for the boolean value to be false.

argument

When Python executes this line, you say that Python is calling the print() function and the string value is being passed to the function. A value that is passed to a function is called an argument. Functions are surrounded by ( )

IDLE

While the Python interpreter is the software that runs your Python programs, the interactive development environment (IDLE) software is where you'll enter your programs, much like a word processor.

syntaxerror

You have broken the grammer rules of python. Something is wrong with how the code is putting together

assignment statement

You'll store values in variables with an assignment statement. An assignment statement consists of a variable name, an equal sign (called the assignment operator), and the value to be stored.

All flow control statements end with

a colon

blank string

a string with no characters in it

Name three Boolean operators

and, or, not

binary boolean operators

and, or: They will always take two boolean values return either true or false only

//

floored division. The integer is rounded away from zero. If a number is positive then just remove the numbers after the decimal point. But if negative round toward zero. Not like normal rounding. I suppose the floor is zero.

Input Function

input(). It waits for the user to enter a string value into the system. It always returns a string value. If you are wanting to math with it you will need to change it to an integer or float first.

continues and breaks are only used

inside while and for loops. Python will give you an error if used anywhere else.

Three functions to change data type

int(), str(), and float()

=

is used to assign a variable

terminate

no more lines to execute, stops running, exits.

Flow control statements

often start with a part called the condition, and all are followed by a block of code called the clause.

Boolean data type

only has two values. True and False

How to start variables

python convention is to start with a lower case letter. Never with a number.

len()

takes a string value and returns an integer value of the string's length

str()

to convert to a string

int()

to convert to an integer. Also useful to round a floating point number down.

42 == "42" are

unequal. Python considers the string and the integer to be different things.

overwriting the variable.

when you assign the variable a new value

ctrl-c

will interrupt a program, if it is going out of control can use to stop the program.


Conjuntos de estudio relacionados

Rational or Irrational? (***WITH EXPLANATIONS***)

View Set

Reglas Para el uso de La C,S, y Z.

View Set

Investment Management Quiz 5 Review

View Set

Insurance Regulation by the Numbers

View Set

A&P Reproduction Review for Final

View Set

Sociology Chapters 7, 9, 10, 11, and 12

View Set

Toxicology Assessment cont. (Lectures 19-20)

View Set