Python Notes
Python Notes
Heads up!
The print statement is always followed by parentheses
containing the output we want to generate.
Heads up!
Python code contains lots of references to Monty
Python. So you’ll see "spam" and "eggs" used as
placeholder variables
Heads up!
See the spaces around the plus and minus signs? The
code would work without them–but they make it a lot
easier to read.
Heads Up!
Using a single slash to divide numbers produces a
decimal (or float, as it’s known programming). We'll
dive into floats in the next lesson.
Heads up!
A float can be added to an integer, because Python
automatically converts the integer to a float. Clever
Python!
Heads Up!
You can also use floor division on floats, and the result
will always be a float.
Heads up!
The delimiter (" or ') used for a string doesn't affect
how it behaves in any way.
Heads Up!
Backslashes that we use for escaping, don’t get
displayed in the output.
To create a new line we use \n.
Heads Up!
Based on what we just learned, can you guess how we
represent tab? That’s right, it’s \t.
Heads Up!
But don’t try to add a string to a number! Even though
they might look similar, they are two different entities,
so doing this will break the code and produce an error
Multiplying a string by an integer, produces a repeated
version of the original string.
Heads Up!
But don’t try to multiply a string by another string. This
will just generate an error. The same will happen if you
try to multiply a string by a float, even if the float is a
number.
Heads Up!
But Beware! Overwriting the same variable with
different data types is not good practice. To avoid
mistakes, try to avoid doing it.
Heads Up!
Even if the user enters a number as input, it’s
processed as a string.
Heads Up!
When the input() function executes the program flow
stops until a user enters some value. Ball’s in your
court user!
Heads Up!
Be careful not to confuse assignment (one equals sign)
with comparison (two equals signs).
Heads Up!
Comparison operators are also called Relational
operators
Heads Up!
Python uses indentation (that empty space at the
beginning of a line) to delimit blocks of code.
Depending on the program's logic, indentation can be
mandatory. As you can see, the statements in the if
should be indented.
Heads Up!
The colon at the end of the expression in the if
statement is important, don’t leave it out.
Like if x>4:
Heads Up!
Indentation is used to define the level of nesting
Heads Up!
The colon at the end else keyword is important, don’t
leave it out.
Heads Up!
Indentation determines which if/else statements the
code blocks belong to.
Heads Up!
The elif statement is equivalent to an else/if
statement. It’s used to make the code shorter, more
readable, and avoid indentation increase.
Heads Up!
str(x) is used to convert the number x to a string, so
that it can be used for concatenation.
Heads Up!
The string "Hello" can be thought of as a list, where
each character is an item in the list. The first item is
"H", the second item is "e", and so on.
Heads Up!
The in operator is also used to determine whether or
not a string is a substring of another string.
Heads Up!
Similar to while loops, the break and continue
statements can be used in for loops, to stop the loop
or jump to the next iteration.
Heads Up!
Remember, the second argument is not included in the
range, so range(3, 8) won’t include the number 8.
Heads Up!
Want to go backward? No problem! We can also create
a list of decreasing numbers, using a negative number
as the third argument, for example list(range(20, 5,
-2)).