Table of Contents
These are my notes while taking the Complete Python Developer course by Andrei Neagoie on ZTM Academy.
Right away, I am impressed with the Python site by giving you up front details and tutorials on how to learn the language.
Python for Programmers Tutorials
Disclaimer: I came from a JavaScript background and this will be starting from a more advanced level than someone who is new to programming. The links above do go through some more of the basics. Also, fair warning for when I start comparing things to JavaScript 😏
Python is:
- strongly typed - types are enforced
- dynamic - checked during execution, not before
- implicit - variables do not need to be declared
- object-oriented - everything is an object
Syntax
Python does not have a mandatory character (like semicolon ;) to close off a statement, the end of a statement is marked by a newline character. To continue a statement to multiple lines, use the line continuation character (\). Blocks are specified by four spaces or a tab indentation, not including the indentation will result in a syntax error. Statements that need an indentation after end in a colon (:). Failing to indent a block of code will result in a syntax error. Comments start with the pound or hashtag (#) symbol for a single line and 3 string characters (''') to start and end the block for multi-line comments. Assignment is done with the equals (=) sign, and testing equality is done with a double equals (==). You can also shortcut increment/decrement characters with (+=/-=).
a = 5 #no semicolon here
b = 2
if a > b:
print(a 'is greater than' b)
elif a == b:
print(a 'is equal to' b)
else:
print(a 'is less than' b))
'''
not indenting the print line
will give a syntax error
'''
# as long as the string comment
# isn't set to a variable,
# python will ignore it
Variables
Rules for naming Python variables:
- ◾ Must start with a letter or underscore (_) character.
- ◾ Cannot start with a number.
- ◾ Can only contain alpha-numeric characters and underscores.
- ◾ Names are case-sensitive.
Multiple values can also be assigned in one line.
x, y, z = 'one', 'two', 'three'
print(x)
print(y)
print(z)
# one
# two
# three
Constants can be declared in Python using all caps and underscores (_). Constants are typically declared in a separate file and then imported back into the main file.
# constant.py
PI = 3.14
GRAVITY = 9.8
# main.py
import constant
print(constant.PI) # 3.14
print(constant.GRAVITY) # 9.8
Operators
Math Operators
◾ + Addition, adds two numbers.
◾ - Subtraction, subtracts two numbers.
◾ * Multiplication, multiplies two numbers.
◾ / Division (float), divides first number by the second number with remainders as floating point number.
◾ // Division (floor), divides first number by the second number rounded to the nearest number.
◾ % Modulus, returns the remainder of the first number divided by the second number.
# Examples of Arithmetic Operator
a = 9
b = 4
add = a + b
sub = a, b
mul = a * b
div1 = a / b
div2 = a // b
mod = a % b
print(add) # 13
print(sub) # 5
print(mul) # 36
print(div1) # 2.25
print(div2) # 2
print(mod) # 1
Relational Operators
◾ > Greater than, returns true if left is greater than the right, else false.
◾ < Less than = return true if left is less than the right, else false.
◾ == Equal to, true if both sides are equal and of the same type.
◾ != Not equal to, true if sides are not equal, else false.
◾ >= Greater than or equal to, true if the left is greater or equal to the right.
◾ <= Less than or equal to, true if the left is less than or equal to the right.
# Examples of Relational Operators
a = 13
b = 33
print(a > b) # False
print(a < b) # True
print(a == b) # False
print(a != b) # True
print(a >= b) # False
print(a <= b) # True
Logical Operators
◾ and AND, true if both or all conditions are true, else false.
◾ or OR, true if any condition is true, else false.
◾ not NOT, true if the condition is false, else true.
# Examples of Logical Operator
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
Python also has global or local variables. Any variable created outside of a function will be global and accessible anywhere. A variable created inside of a function will be local and will only be accessible within that function. The keyword global can be used on a variable inside of a function to make it accessible from the outside or to allow changes in a function to a variable declared on the global scope.
Types
Python has many different types that are able to do different things based on their type. To get the type of a variable, you can run type(var).
Text | str |
Numeric: | int , float , complex |
Sequence: | list , tuple , range |
Mapping | dict |
Set: | set , frozenset |
Boolean | bool |
Binary: | bytes , bytearray , memoryview |
Example | Data Type |
---|---|
x = "Hello World" | str |
x = 20 | int |
x = 20.5 | float |
x = 1j | complex |
x = ["apple", "banana", "cherry"] | list |
x = ("apple", "banana", "cherry") | tuple |
x = range(6) | range |
x = {"name" : "John", "age" : 36} | dict |
x = {"apple", "banana", "cherry"} | set |
x = frozenset({"apple", "banana", "cherry"}) | frozenset |
x = True | bool |
x = b"Hello" | bytes |
x = bytearray(5) | bytearray |
x = memoryview(bytes(5)) | memoryview |
Keywords
False | class | finally | is | return |
None | continue | for | lambda | try |
True | def | from | nonlocal | while |
and | del | global | not | with |
as | elif | if | or | yield |
assert | else | import | pass | |
break | except | in | raise |