Comments within a file allow us to write a note. These are just statements which is ignored during program execution. In Python, you can add a single-line comment with the
# symbol or a multi-line comment by surrounding the text with
Comments are used often to describe what a function does, what parameters it takes in and other details which provides an easy-to-understand summary for another developer when he or she looks at the code.
Triple quotes are especially used as docstrings. Docstrings appear as the first statement in a module, class or function. This is a convenient way for us to document our code as we program.
We can either do a one-line docstring or a multi-line docstring.
The above snippet is an example of a one-line docstring. Note that even though our description is just a one-liner, we have still used the triple quotes. There are also no lines before/after the quotes.
The above is a multi-line docstring, which takes up more lines to describe the arguments.
How do we store data? We use variables. To store an integer, we simply say
x=2 . To store a name, ‘Jack’, we say
name=‘Jack’. However, the variable here do not` contain the objects themselves. They are merely labels which points to the object.
This means that we can easy make the variable point to something else.
x=3 will now change the reference of
x to point to
We talked about two variables above: an integer and a string. Does Python know they are an integer and a string? No.
Python is a dynamically-typed language. This means that each variable does not have a specific type. If you are coming from statically-typed language like C or Java, you might know that you will have to declare a specific type like
int when declaring variables. That concept is not present in Python.
Give it a try!
The above code should print
hello. Now try re-assigning
x to some other data, maybe an integer and print it.
Following Python conventions, we will be using the snake_case to name our variables. This means connecting two or more word with the underscore character. This is important for readability of the code.
Make sure your variable names are descriptive. Try to avoid names like
x, which does not give any idea of the data being stored in it. If you are storing the first name of yourself, give it a name
first_name. Variable names must always be made as clear and concise as possible.
Functions are going to be a part of every program you write. Basically, functions take in some parameters and give you an output. Within the function, you have an algorithm to describe what the function should do with the various parameters we have provided.
Let’s take an example:
The above is a very simple function. Like the name,
add_one suggests, we simply add 1 to whatever argument the function receives and return it. Executing
add_one(2) will yield
3. When you call
2 here is the argument provided to the function.
Parameters are what a function takes in. Arguments are what is provided to the function at the time of calling it.
A slightly more complex function. Let’s convert Celsius to Fahrenheit.
According to Celsius to Fahrenheit formula - How to convert Celsius to Fahrenheit (°F), we need to multiply the temperature in Celsius by 9/5 and then add 32 to it. Let’s write a function to do this.
Short and sweet right? Now, let’s execute this by calling
32 is the argument we are providing to the function, which represents 32 degree Celsius. Executing this call will return 89.6, which is the Fahrenheit equivalent.
Exercise 2.1 Now, try writing a function to convert miles to kilometres.
Exercise 2.2 Now, write a function that returns the area of a circle when given the radius as a function parameter. Be sure to name the function and variables expressively with snake_case.
In the last part of this section, we are going to cover more on variables - where should they be defined and where should they be used.
You can define variables at the global level, meaning these variables can be referenced from anywhere. For example:
The above code returns the average marks for a class of 10 students. The number “10” is stored at a global level, which is outside the function. Hence, it can be referenced within the function’s scope. In this example, the variable
num_of_students exists in the global namespace.
We can also specify variables at the local level, i.e. within functions. For example:
In the above example, we have defined
a='hello within the
print_hello() function. As such, we can only use
a within the function. If you try to reference
a anywhere outside the function, it will raise
NameError: name 'a' is not defined.
So what are namespaces?
A namespace is a mapping from names to objects.
In Python, namespaces can be created at different times with different lifetimes. When the Python interpreter starts, it creates a built-in namespace. Next, when the module (i.e. the file the code resides in) is loaded, a global namespace is created. Finally, when you call a function, a local namespace is created. This local namespace is deleted once we return from the function or when it raises an exception and the program terminates.
When you use a variable, Python first checks if it is defined in the local, followed by global and then finally the built-in namespaces. If it does not find the variable, then it raises a