Basic Python

Basic Components 🐍

Python follows basic order of operations when evaluating expressions, similar to PEMDAS for math.

Operator

Operation

Example

Evaluates to

**

Exponent

2**4

16

%

Modulus (remainder!)

5 % 2

1

//

Integer Division (floored)

11 // 2

5

/

Division

11 / 2

5.5

*

Multiplication

2 * 3

6

-

Subtraction

3 - 2

1

+

Addition

2 + 4

6

Data Types and Classifications πŸ‘©β€πŸ«

Using a single equals = is an assignment operator, where it assigns a value to a variable.

Using double equals == is a comparison operator, where it checks two values!

Flow Control πŸŒŠπŸ€½β€β™€οΈ

Tons of different ways to control execution of the program. Fundamental way in how programs work and look like magic ✨ Until you realize that whitespace is interpreted. Make sure you use four spaces instead of tabs, just as God intended.

# if, elif, and else control!!
ayyy = 'lmao'

# if statements check if a statement is true
if ayyy == 'lmao':
    print('πŸ‘½πŸ‘½πŸ‘½')

# elif statments help  determine one of many possible clauses
elif len(ayyy) == 4:
    print('πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚')

# can have have several elif, but skips any remaining elif after first True
elif len(ayyy) != 4:
    print('πŸ€”πŸ€”πŸ€”')

# else statements is executed ONLY if the preceeding if is False (NOT REQUIRED!)
else:
    print('βŒπŸ‘½βŒ')

There are while loops which will check for a condition and keep executing until completed. Using break will exit the while loop early! Similarly, continue statements will jump back to the beginning of a while loop and evaluate the loop's condition.

Another loop is the for loop which can repeat an action a specific amount of times. When using range we can also specify start, stop, and steps.

Strings 🧡

We can take a string of any length and reference ANY part of it!

Lists πŸ“œπŸ“œ

Comprehension

Search for a partial match in a list and add to another list. Super useful!

Tuples

Dictionaries πŸ“šπŸ“š

Very similar to JSON. In fact, so close, it can be converted to JSON with minor worries!

User Inputs

Loops

Arithmetic and Conditionals

Regex Matching

Convert bytes to IPv4

Last updated

Was this helpful?