You can add new items to a list using the append() and insert() methods .
>>> spam = ['cat', 'dog', 'bat'] >>> spam.append('moose') # <- append >>> spam ['cat', 'dog', 'bat', 'moose'] >>> spam.insert(1, 'chicken') # <- insert >>> spam ['cat', 'chicken', 'dog', 'bat', 'moose'] Note the list is modified in place. Unlike a function it doesn’t always return a value.
Arguments are what’s assigned to a parameter . Arguments are often passed to functions .
Instead of:
>>> spam = 42 >>> spam = spam + 42 >>> spam 84 You can use shortcuts also known as augmented assignment operators.
>>> spma = 42 >>> spma += 1 >>> spma 43 Table of augmented assignment operators # Augmented assignment statement Equivalent assignment statement spam += 1 spam = spam + 1 spam -= 1 spam = spam -1 spam *= 1 spam = spam * 1 spam /= 1 spam = spam / 1 spam %= 1 spam = spam % 1
The three Boolean operators are:
and or not These are used to compare Boolean values .
Binary Boolean Operators # The and and or operators always take two Boolean values so they’re considered binary operators.
The and Operator’s Truth Table
Expression Evaluates to … True and False False False and True False False and False False The or Operator’s Truth Table
...
When a while or for loop’s clause reaches a break statement it exits the loop.
while True: print('Please type your name.') name = input() if name == 'your name': break print('Thank you!')
See: in and not operators
Sometimes it’s useful to have a class which is a specialized version of another class. To do this, you can use inheritance. In this case the child class will inherit attributes and methods from the parent class.
Inheritance # To inherit things from the parent class call the __init__() method from the parent class. Call your parent.
class Car: """A simple attempt to represent a car.""" def __init__(self, make, model, year): """Initialize attributes to describe a car.
...
Lines of Python code are grouped into blocks.
Three rules for code blocks:
Blocks begin when indentation increases Blocks can contain other blocks Blocks end when indentation decreases to zero or containing block’s indentation
: Common Data Types
Data type Examples Integers -2, -1, 0, 1, 2 Floating-point numbers -1.25, -1.0, -0.5, 0.0, 0.5, 1.0, 1.25 Strings ‘a’, ‘aa’, ‘aaa’, ‘Hello!’, ‘11 cats’
Conditions (aka expressions) evaluate to a boolean value of True or False.
In the context of flow control statements expressions are called conditions. A flow control statement decides what to do based on whether its condition is True or False.
Conditions are always followed by a block of code called a clause .