Skip to content

Python - From Scratch to Expert

Python utilizes the PEMDAS rule for arithmetic operations, i.e., Parenthesis Exponential Multiplication Division Addition Subtraction, in that order (top to bottom).

OperatorNameDescription
a + bAdditionSum of a and b
a - bSubtractionDifference of a and b
a * bMultiplicationProduct of a and b
a / bTrue divisionQuotient of a and b
a // bFloor divisionQuotient of a and b, removing fractional parts
a % bModulusInteger remainder after division of a by b
a ** bExponentiationa raised to the power of b
-aNegationThe negative of a

In Python, comments are denoted using # at the beginning.

Python function template - def function_name (input_variable): ---computational statements--- return output_variable

In Python, scope is managed/defined using indentations (tab-space) with local/global scope applicable (inside a function/class or global declaration)

Peculiar and Important Factors around Data Types

  • Cannot Multiply String with Float (but String * Integer works)
  • Type casting is possible, however it does not “automatically” do calculations for complicated conversions (character string to float does not work, but numerical string to float will)
  • Strings can have 0 length (empty quotation marks)
  • Boolean data type can be utilized using conditional operators in assignment (>, <, not)

Conditional Operators

SymbolMeaning
==equals
!=does not equal
<less than
<=less than or equal to
>greater than
>=greater than or equal to

List Slicing -

  • to pull the first x entries, you use [:x]
  • to pull the last y entries, you use [-y:]

Docstrings (""" """) are utilized to provide descriptions for functions (defined)

Print statement in python have an attribute (sep) that can be utilized. Default is a single space.

It is possible to add default values to functions input_variable to avoid errors.

OperatorDescription
(expressions...),

[expressions...]{key: value...}{expressions...}
Binding or parenthesized expression, list display, dictionary display, set display
x[index]x[index:index]x(arguments...)x.attributeSubscription, slicing, call, attribute reference
await xAwait expression
**Exponentiation 5(https://docs.python.org/3/reference/expressions.html#id23)
+x-x~xPositive, negative, bitwise NOT
*@///%Multiplication, matrix multiplication, division, floor division, remainder 6(https://docs.python.org/3/reference/expressions.html#id24)
+-Addition and subtraction
<<>>Shifts
&Bitwise AND
^Bitwise XOR
|Bitwise OR
innot inisis not<<=>>=!=, `==Comparisons, including membership tests and identity tests
not xBoolean NOT
andBoolean AND
orBoolean OR
if – elseConditional expression
lambdaLambda expression
:=Assignment expression

In python, attributes can be called using dot (.) with attribute name (e.g., imag). Methods can also be called with variables but require parentheses at the end (e.g., bit_length() )

Zen of Python: Readability counts.
Explicit is better than implicit.

String helper-

What you type…What you getexampleprint(example)
\'''What\'s up?'What's up?
\"""That's \"cool\""That's "cool"
\\\"Look, a mountain: /\\"Look, a mountain: /\
\n"1\n2 3"1
2 3

Strings are very similar to lists but are immutable.

Dictionaries are a built-in data structure that form {‘key’ : value} pairs