Python is a dynamic and strongly typed programming language. It employs both duck typing and gradual typing via type hints.
While Python supports many different programming styles, internally everything in Python is an object. This includes numbers, strings, lists, and even functions.
We'll dig more into what all of that means as we continue through the track.
This first exercise introduces 4 major Python language features:
- Name Assignment (variables and constants),
- Functions (the
defkeyword and thereturnkeyword), - Comments, and
- Docstrings.
In general, content, tests, and analyzer tooling for the Python track follow the style conventions outlined in [PEP 8](https://www.python.org/dev/peps/pep-0008/) and [PEP 257](https://www.python.org/dev/peps/pep-0257/) for Python code style, with the additional (strong) suggestion that there be no single letter variable names or variables named ["_"][uses of _ in Python].
On the Python track, [variables][variables] are always written in [`snake_case`][snake case], and constants in `SCREAMING_SNAKE_CASE`.
[variables]: https://realpython.com/python-variables/
[snake case]: https://en.wikipedia.org/wiki/Snake_case
[uses of _ in Python]: https://medium.com/better-programming/how-to-use-underscore-properly-in-python-37df5e05ba4c
Programmers can bind names (also called variables) to any type of object using the assignment = operator: <name> = <value>.
A name can be reassigned (or re-bound) to different values (different object types) over its lifetime.
>>> my_first_variable = 1 #<-- my_first_variable bound to an integer object of value one.
>>> my_first_variable = 2 #<-- my_first_variable re-assigned to integer value 2.
>>> print(type(my_first_variable))
<class 'int'>
>>> print(my_first_variable)
2
>>> my_first_variable = "Now, I'm a string." #<-- You may re-bind a name to a different object type and value.
>>> print(type(my_first_variable))
<class 'str'>
>>> my_first_variable = 'You can call me "str".' #<-- Strings can be declared using single or double quote marks.
>>> print(my_first_variable)
You can call me "str".Constants are names meant to be assigned only once in a program.
They should be defined at a module (file) level, and are typically visible to all functions and classes in the program.
Using SCREAMING_SNAKE_CASE signals that the name should not be re-assigned, or its value mutated.
The def keyword begins a function definition.
Each function can have zero or more formal parameters in () parentheses, followed by a : colon.
Statements for the body of the function begin on the line following def and must be indented in a block.
# The body of a function is indented by 2 spaces, & prints the sum of the numbers.
def add_two_numbers(number_one, number_two):
total = number_one + number_two
print(total)
>>> add_two_numbers(3, 4)
7
# Inconsistent indentation in your code blocks will raise an error.
>>> def add_three_numbers_misformatted(number_one, number_two, number_three):
... result = number_one + number_two + number_three # This was indented by 4 spaces.
... print(result) #this was only indented by 3 spaces
...
...
File "<stdin>", line 3
print(result)
^
IndentationError: unindent does not match any outer indentation levelFunctions explicitly return a value or object via the return keyword:
# Function definition on first line, explicit return used on final line.
>>> def add_two_numbers(number_one, number_two):
return number_one + number_two
# Calling the function in the Python shell returns the sum of the numbers.
>>> add_two_numbers(3, 4)
7
# Assigning the function call to a variable and printing it
# will also return the value.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
11Functions that do not have an explicit expression following a return will implicitly return the None object.
The details of None will be covered in a later exercise.
For the purposes of this exercise and explanation, None is a placeholder that represents nothing, or null:
# This function will return `None`
def square_a_number(number):
square = number * number
return # <-- note that this return is not followed by an expression
# Calling the function in the Python shell appears
# to not return anything at all.
>>> square_a_number(2)
>>>
# Using print() with the function call shows that
# the function is actually returning the **None** object.
>>> print(square_a_number(2))
NoneFunctions that omit return will also implicitly return the None object.
This means that if you do not use return in a function, Python will return the None object for you.
# This function omits a return keyword altogether
def add_two_numbers(number_one, number_two):
result = number_one + number_two
>>> add_two_numbers(5, 7)
>>> print(add_two_numbers(5, 7))
None
# Assigning the function call to a variable and printing
# the variable will also show None.
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
NoneFunctions are called or invoked using their name followed by ().
Dot (.) notation is used for calling functions defined inside a class or module.
>>> def raise_to_power(number, power):
return number ** power
...
>>> raise_to_power(3,3) # Invoking the function with the arguments 3 and 3.
27
# A mismatch between the number of parameters and the number of arguments will raise an error.
>>> raise_to_power(4,)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: raise_to_power() missing 1 required positional argument: 'power'
# Calling methods or functions in classes and modules.
>>> start_text = "my silly sentence for examples."
>>> str.upper(start_text) # Calling the upper() method from the built-in str class on start_text.
'MY SILLY SENTENCE FOR EXAMPLES.'
# Importing the math module
import math
>>> math.pow(2,4) # Calling the pow() function from the math module
16.0Comments in Python start with a # that is not part of a string, and end at line termination.
Unlike many other programming languages, Python does not support multi-line comment marks.
Each line of a comment block must start with the # character.
The first statement of a function body can optionally be a docstring, which concisely summarizes the function or object's purpose. Docstrings are declared using triple double quotes (""") indented at the same level as the code block:
# An example from PEP257 of a multi-line docstring.
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0:
return complex_zeroDocstrings are read by automated documentation tools and are returned by calling the special attribute .__doc__ on the function, method, or class name.
Docstring conventions are laid out in PEP257.
Docstrings can also function as lightweight unit tests, which will be covered in a later exercise.
# An example on a user-defined function.
>>> def raise_to_power(number, power):
"""Raise a number to an arbitrary power.
:param number: int the base number.
:param power: int the power to raise the base number to.
:return: int - number raised to the specified power.
Takes a number and raises it to the specified power, returning the result.
"""
return number ** power
...
# Calling the .__doc__ attribute of the function and printing the result.
>>> print(raise_to_power.__doc__)
Raise a number to an arbitrary power.
:param number: int the base number.
:param power: int the power to raise the base number to.
:return: int - number raised to the specified power.
Takes a number and raises it to the specified power, returning the result.