1/63
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
statement
Specifies a task to perform
In: 45+72
Ou: 117
assignment statement
creates a variable and uses the assignment symbol (=) to give variable a value
x=17
Adding Variable Values and Viewing the Result
In: x + y
Out: 10
assignment symbol
=
Variable Names
An identifier. May consist of letters, digits and underscores (_) but may not begin with a digit. Python is case sensitive.
A function performs a task when you call it by writing its name, followed by
parentheses ()
type() function
returns the type of the specified object. The parentheses contain the function’s argument—the data that the type function needs to perform its task
/
True division. Divides a numerator by a denominator and yields a floating-point number
//
Floor division. Divides a numerator by a denominator, yielding the highest integer that’s not greater than the result. Rounds down and truncates (discards) the fractional part.
%
Remainder operator. Yields the remainder from division
*
Multiplication operator
**
Exponentiation operator. Raises one value to the power of another
** (0.5)
Calculates square root
ZeroDivisionError
Dividing by zero with / or // is not allowed and results in an exception
NameError
An exception occurs if you try to use a variable that you have not yet created
Algebraic expressions must be typed in
straight-line form using Python’s operators.
Grouping Expressions with Parentheses
Parentheses group Python expressions, as in algebraic expressions
What has the highest level of precedence in Python expressions?
Expressions in parentheses evaluate first, so parentheses may force the order of evaluation to occur in any sequence you desire. Parentheses have the highest level of precedence. In expressions with nested parentheses, such as (a / (b - c)), the expression in the innermost parentheses (that is, b - c) evaluates first.
What operations evaluate after parentheses?
Exponentiation operations evaluate next. If an expression contains several exponentiation operations, Python applies them from right to left.
What operations come after exponentiation?
Multiplication, division and modulus operations evaluate next. If an expression contains several multiplication, true-division, floor-division and modulus operations, Python applies them from left to right. Multiplication, division and modulus are “on the same level of precedence.”
What operations evaluate last?
Addition and subtraction operations evaluate last. If an expression contains several addition and subtraction operations, Python applies them from left to right. Addition and subtraction also have the same level of precedence.
All Python operators of the same precedence group left-to-right except for
the exponentiation operator **, which groups right-to-left.
If both operands are integers,
the result is an integer—except for the true-division ( / ) operator, which always yields a floating-point number.
If both operands are floating-point numbers,
the result is a floating-point number.
Mixed-type (float and integer) expressions produce
floating-point results.
print() function
prints the specified message to the screen
Strings
Can be enclosed in single ‘ ‘ or double “ “ quotes
Printing a Comma-Separated List of Items
Displays each argument separated from the next by a space
In: print(’Welcome’, 'to’, 'Python!')
Out: Welcome to Python!
A backslash ( \ ) in a string is
the escape character. The backslash and the character immediately following it form an escape sequence.
\n
Insert a newline character in a string. When the string is displayed, for each newline, move the screen cursor to the beginning of the next line
\t
Insert a horizontal tab. When the string is displayed, move the screen cursor to the next tab stop for each tab.
\\
Insert a backslash character in a string.
\”
Insert a double quote character in a string.
\’
Insert a single quote character in a string.
Split a long string (or a long statement) over several lines by using
the \ continuation character as the last character on a line to ignore the line break.

Triple-Quoted Strings
‘‘‘ or “““
Used for:
multiline strings
strings containing single or double quotes
docstrings—the recommended way to document the purposes of certain program components.
A string delimited by single quotes
may include double-quote characters, but not single quotes, unless you use the \' escape sequence.
A string delimited by double quotes
may include single quote characters, but not double quotes, unless you use the \" escape sequence.
Triple-quoted strings may contain
both single and double quotes.
Multiline Strings
IPython knows that the string is incomplete because we did not type the closing ,””” before we pressed Enter.
IPython displays a continuation prompt ...: at which you can input the multiline string’s next line.
This continues until you enter the ending """ and press Enter.
Python stores multiline strings with embedded newline characters.

input()
requests and obtains user input
input always returns a
string
string concatenation
" " + " "
creates a new string containing the left operand’s value followed by the right operand’s value
Getting an integer from the input
convert the string to an integer using the built-in int function.
Ex: int(input("What is your age?: ")
If the string passed to int cannot be converted to an integer,
a ValueError occurs
int() can also convert
a float into an int
A condition is a Boolean expression with the value
True or False
Comparison operators
>
<
>=
<=
all have a higher precedence than
==
!=
beginning with the hash character #
indicates that the rest of the line is a comment. You insert comments to document your code and to improve readability. Comments also help other programmers read and understand your code. They do not cause the computer to perform any action when the code executes.
Docstrings
''' ''' explains the script’s purpose.
Used to describe script components you define, such as new functions and new types called classes
White space
blank lines, space characters, and tab characters
Typically, you write statements on one line. You may spread a lengthy statement over several lines with the
\ continuation character. Python also allows you to split long code lines in parentheses without using continuation characters
Each if statement consists of the
keyword if, the condition to test, and a colon (:) followed by an indented body called a suite. Each suite must contain one or more statements.
Python requires you to indent the statements in
suites
read == as
“is equal to”
read = as
“is assigned to”
You can chain comparisons to check whether
a value is in a range
Ex: 1<=x<=5
int
Integers
float
Floating-point numbers
str
Strings
Every object has a
type and a value
Assigning an object to a variable
binds (associates) that variable’s name to the object. You can then use the variable in your code to access the object’s value
dynamic typing
it determines the type of the object a variable refers to while executing your code
garbage collection
Python creates objects in memory and removes them from memory as necessary. This ensures that memory is available for new objects you create