Basic Python Revision Notes
With help from Nitish Mittal
HELP from Documentation
dir(module)
help()
Important Characters and Sets of Characters
tab \t
new line \n
backslash \\
string " " or ' '
docstring """ """
comparison operators == , < , > , <= , >= , !=
Python type boolean True , False.
Logical operators not , and , or
Order of Operations (from Emory)
Operator Description
() Parentheses (grouping)
f(args...) Function call
x[index:index] Slicing
x[index] Subscription
x.attribute Attribute reference
** Exponentiation
+x, -x Positive, negative
*, /, % Multiplication, division, remainder
+, - Addition, subtraction
in, not in, is, is not, <, <=, >, >=, Comparisons, membership, identity
<>, !=, ==
not x Boolean NOT
and Boolean AND
or Boolean OR
, Variable Names
case sensitive
cannot start with a number (ex, 1_assd is not allowed)
Six Steps to Defining a Function
1. What should your function do? Type a couple of example calls.
2. Pick a meaningful name (often a verb or verb phrase): What is a short answer to "What does
your function do"?
3. Decide how many parameters the function takes and any return values
4. Describe what your function does and any parameters and return values in the docstring
5. Write the body of the function
6. Test your function. Think about edge cases.
Integers and Strings
>>> int(45)
45
>>> int('45')
45
>>> str(45)
'45'
>>> str('45')
'45'
>>> int(str(45))
45
Calling Methods
module_name.function_name(x)
math.sqrt(x)
random.randrange(2,5)
Conditionals and Branching
if
elif
else
We have a boolean logic expression for if which works when the Boolean evaluates to True
With help from Nitish Mittal
HELP from Documentation
dir(module)
help()
Important Characters and Sets of Characters
tab \t
new line \n
backslash \\
string " " or ' '
docstring """ """
comparison operators == , < , > , <= , >= , !=
Python type boolean True , False.
Logical operators not , and , or
Order of Operations (from Emory)
Operator Description
() Parentheses (grouping)
f(args...) Function call
x[index:index] Slicing
x[index] Subscription
x.attribute Attribute reference
** Exponentiation
+x, -x Positive, negative
*, /, % Multiplication, division, remainder
+, - Addition, subtraction
in, not in, is, is not, <, <=, >, >=, Comparisons, membership, identity
<>, !=, ==
not x Boolean NOT
and Boolean AND
or Boolean OR
, Variable Names
case sensitive
cannot start with a number (ex, 1_assd is not allowed)
Six Steps to Defining a Function
1. What should your function do? Type a couple of example calls.
2. Pick a meaningful name (often a verb or verb phrase): What is a short answer to "What does
your function do"?
3. Decide how many parameters the function takes and any return values
4. Describe what your function does and any parameters and return values in the docstring
5. Write the body of the function
6. Test your function. Think about edge cases.
Integers and Strings
>>> int(45)
45
>>> int('45')
45
>>> str(45)
'45'
>>> str('45')
'45'
>>> int(str(45))
45
Calling Methods
module_name.function_name(x)
math.sqrt(x)
random.randrange(2,5)
Conditionals and Branching
if
elif
else
We have a boolean logic expression for if which works when the Boolean evaluates to True