`main()`

FunctionThe `main()`

function is used to define the starting point of our program. Including the `main()`

function allows us to import and run this program in another script.

def main():print('Hello World!')if __name__ == '__main__':main()

A comment is a piece of text within a program that is not executed. It can be used to provide additional information to aid in understanding the code.

The `#`

character is used to start a comment and it continues until the end of the line.

# Comment on a single lineuser = "JDoe" # Comment after code

`print()`

FunctionThe `print()`

function is used to output text, numbers, or other printable information to the console.

It takes one or more arguments and will output each of the arguments to the console separated by a space. If no arguments are provided, the `print()`

function will output a blank line.

print("Hello World!")print(100)pi = 3.14159print(pi)

A variable is used to store data that will be used by the program. This data can be a number, a string, a Boolean, a list or some other data type. Every variable has a name which can consist of letters, numbers, and the underscore character `_`

.

The equal sign `=`

is used to assign a value to a variable. After the initial assignment is made, the value of a variable can be updated to new values as needed.

# These are all valid variable names and assignmentuser_name = "codey"user_id = 100verified = False# A variable's value can be changed after assignmentpoints = 100points = 120

The Python `type()`

function returns the data type of the argument passed to it.

a = 1print(type(a)) # <class 'int'>a = 1.1print(type(a)) # <class 'float'>a = 'b'print(type(a)) # <class 'str'>a = Noneprint(type(a)) # <class 'NoneType'>

Python supports different types of arithmetic operations that can be performed on literal numbers, variables, or some combination. The primary arithmetic operators are:

`+`

for addition`-`

for subtraction`*`

for multiplication`/`

for division`%`

for modulus (returns the remainder)`**`

for exponentiation

# Arithmetic operationsresult = 10 + 30result = 40 - 10result = 50 * 5result = 16 / 4result = 25 % 2result = 5 ** 3

`+=`

The plus-equals operator `+=`

provides a convenient way to add a value to an existing variable and assign the new value back to the same variable. In the case where the variable and the value are strings, this operator performs string concatenation instead of addition.

The operation is performed in-place, meaning that any other variable which points to the variable being updated will also be updated.

# Plus-Equal Operatorcounter = 0counter += 10# This is equivalent tocounter = 0counter = counter + 10# The operator will also perform string concatenationmessage = "Part 1 of message "message += "Part 2 of message"

`%`

A modulo calculation returns the remainder of a division between the first and second number. For example:

- The result of the expression
`4 % 2`

would result in the value 0, because 4 is evenly divisible by 2 leaving no remainder. - The result of the expression
`7 % 3`

would return 1, because 7 is not evenly divisible by 3, leaving a remainder of 1.

# Modulo operationszero = 8 % 4nonzero = 12 % 5

`or`

OperatorThe Python `or`

operator combines two Boolean expressions and evaluates to `True`

if at least one of the expressions returns `True`

. Otherwise, if both expressions are `False`

, then the entire expression evaluates to `False`

.

True or True # Evaluates to TrueTrue or False # Evaluates to TrueFalse or False # Evaluates to False1 < 2 or 3 < 1 # Evaluates to True3 < 1 or 1 > 6 # Evaluates to False1 == 1 or 1 < 2 # Evaluates to True

`==`

The equal operator, `==`

, is used to compare two values, variables or expressions to determine if they are the same.

If the values being compared are the same, the operator returns `True`

, otherwise it returns `False`

.

The operator takes the data type into account when making the comparison, so a string value of `"2"`

is *not* considered the same as a numeric value of `2`

.

# Equal operatorif 'Yes' == 'Yes':# evaluates to Trueprint('They are equal')if (2 > 1) == (5 < 10):# evaluates to Trueprint('Both expressions give the same result')c = '2'd = 2if c == d:print('They are equal')else:print('They are not equal')

`!=`

The Python not equals operator, `!=`

, is used to compare two values, variables or expressions to determine if they are NOT the same. If they are NOT the same, the operator returns `True`

. If they are the same, then it returns `False`

.

The operator takes the data type into account when making the comparison so a value of `10`

would NOT be equal to the string value `"10"`

and the operator would return `True`

. If expressions are used, then they are evaluated to a value of `True`

or `False`

before the comparison is made by the operator.

# Not Equals Operatorif "Yes" != "No":# evaluates to Trueprint("They are NOT equal")val1 = 10val2 = 20if val1 != val2:print("They are NOT equal")if (10 > 1) != (10 > 1000):# True != Falseprint("They are NOT equal")

In Python, *relational operators* compare two values or expressions. The most common ones are:

`<`

less than`>`

greater than`<=`

less than or equal to`>=`

greater than or equal too

If the relation is sound, then the entire expression will evaluate to `True`

. If not, the expression evaluates to `False`

.

a = 2b = 3a < b # evaluates to Truea > b # evaluates to Falsea >= b # evaluates to Falsea <= b # evaluates to Truea <= a # evaluates to True

Exponentiation can be calculated in python using the `**`

operator. In order to use this, you place the base number on the left side of the operator and the power on the right side of the operator. For example 5 to the power of 2 would be written as `5 ** 2`

.

# 2 to the 10th power, or 1024print(2 ** 10)# 8 squared, or 64print(8 ** 2)# 9 * 9 * 9, 9 cubed, or 729print(9 ** 3)# We can even perform fractional exponents# 4 to the half power, or 2print(4 ** 0.5)

`and`

OperatorThe Python `and`

operator performs a Boolean comparison between two Boolean values, variables, or expressions. If both sides of the operator evaluate to `True`

then the `and`

operator returns `True`

. If either side (or both sides) evaluates to `False`

, then the `and`

operator returns `False`

. A non-Boolean value (or variable that stores a value) will always evaluate to `True`

when used with the `and`

operator.

True and True # Evaluates to TrueTrue and False # Evaluates to FalseFalse and False # Evaluates to False1 == 1 and 1 < 2 # Evaluates to True1 < 2 and 3 < 1 # Evaluates to False"Yes" and 100 # Evaluates to True

`not`

OperatorThe Python Boolean `not`

operator is used in a Boolean expression in order to evaluate the expression to its inverse value. If the original expression was `True`

, including the `not`

operator would make the expression `False`

, and vice versa.

not True # Evaluates to Falsenot False # Evaluates to True1 > 2 # Evaluates to Falsenot 1 > 2 # Evaluates to True1 == 1 # Evaluates to Truenot 1 == 1 # Evaluates to False