Codecademy Logo

Basic Syntax with Python

print() Function

The 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.14159
print(pi)

Comments

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 line
user = "JDoe" # Comment after code

Variables

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 assignment
user_name = "codey"
user_id = 100
verified = False
# A variable's value can be changed after assignment
points = 100
points = 120

Python type() function

The Python type() function returns the data type of the argument passed to it.

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

Arithmetic Operations

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 operations
result = 10 + 30
result = 40 - 10
result = 50 * 5
result = 16 / 4
result = 25 % 2
result = 5 ** 3

Plus-Equals Operator +=

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 Operator
counter = 0
counter += 10
# This is equivalent to
counter = 0
counter = counter + 10
# The operator will also perform string concatenation
message = "Part 1 of message "
message += "Part 2 of message"

Modulo Operator %

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 operations
zero = 8 % 4
nonzero = 12 % 5

or Operator

The 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 True
True or False # Evaluates to True
False or False # Evaluates to False
1 < 2 or 3 < 1 # Evaluates to True
3 < 1 or 1 > 6 # Evaluates to False
1 == 1 or 1 < 2 # Evaluates to True

Equal Operator ==

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 operator
if 'Yes' == 'Yes':
# evaluates to True
print('They are equal')
if (2 > 1) == (5 < 10):
# evaluates to True
print('Both expressions give the same result')
c = '2'
d = 2
if c == d:
print('They are equal')
else:
print('They are not equal')

Not Equals Operator !=

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 Operator
if "Yes" != "No":
# evaluates to True
print("They are NOT equal")
val1 = 10
val2 = 20
if val1 != val2:
print("They are NOT equal")
if (10 > 1) != (10 > 1000):
# True != False
print("They are NOT equal")

Comparison Operators

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 = 2
b = 3
a < b # evaluates to True
a > b # evaluates to False
a >= b # evaluates to False
a <= b # evaluates to True
a <= a # evaluates to True

Exponents

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 1024
print(2 ** 10)
# 8 squared, or 64
print(8 ** 2)
# 9 * 9 * 9, 9 cubed, or 729
print(9 ** 3)
# We can even perform fractional exponents
# 4 to the half power, or 2
print(4 ** 0.5)

and Operator

The 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 True
True and False # Evaluates to False
False and False # Evaluates to False
1 == 1 and 1 < 2 # Evaluates to True
1 < 2 and 3 < 1 # Evaluates to False
"Yes" and 100 # Evaluates to True

not Operator

The 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 False
not False # Evaluates to True
1 > 2 # Evaluates to False
not 1 > 2 # Evaluates to True
1 == 1 # Evaluates to True
not 1 == 1 # Evaluates to False

Python main() Function

The 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()

Learn More on Codecademy