4 Starting in Python
- Identify basic Python Syntax
- Understand markdown and the Jupyter notebook syntax
- Be able to define variables and call functions
- Identify the type of a variable
4.1 Basic Python Syntax
Comments and new lines
Commenting your code, and using lines liberally, are key to making your code easily understandable for others.
A
#
(Hash) is used for single line commentsA
"""
(triple quotes) is used around multiline comments or stringsA
\
(backslash) is used to explicitly break a lineLines can be implicitly continued in some cases, for example when in brackets. We will see this later.
Assigning variables
In Python initialising variables is easy:
Variables are assigned using the = sign
Case sensitive
No need to declare the type explicitly (dynamic typing)
# Initialising my_height in cm
my_height = 176
my_name = "Kavi"
Variable Naming Conventions in Python
When naming variables, it’s important to follow Python’s guidelines for consistency and readability. Here are some key rules to remember:
Case-sensitive: weight and Weight are different variables.
Use lowercase: Separate words with underscores, e.g., weight_kg.
Don’t start with numbers: Variable names cannot begin with a digit.
Stick to letters: Use only letters, underscores, and digits (when needed).
Be descriptive: Choose clear, meaningful names (e.g., asdjks is not helpful).
Avoid overly long names: For example ‘this_is_my_height_in_cm’ is too long.
Don’t use keywords: Avoid using Python function names, class names, or data types as variable names.
For more detailed guidelines, refer to PEP 8 or the Google Python Style Guide.
Indentation
In python indentation is used to define code blocks. We will see more of this later when we get to conditional statements and loops. - Unexpected indents will result in errors. - indents are 4 spaces according to PEP 8 standards, however commonly also two spaces on web interfaces. Be consistent. - Using spaces is preferable to using tabs
# Initialising my_height in cm
my_height = 176
my_name = "Kavi"
The above would produce an error
4.2 Calling a function
Functions are chunks of code which produce an output. They can (but do not have to) take user defined inputs. Python ships with many in built functions. See: https://docs.python.org/3/library/functions.html
To call a function, simply use the function name followed by parentheses. If the function takes parameters, provide the arguments inside the parentheses. - functions may take arguments in an order separated by a comma:
function(a, b)
- alternatively functions may use assignment for inputs:
function(a, type = b)
- variable inputs into functions can defined globally
b = number
function(a, type = b)
Functions can have default inputs if none specified
functions can be nested although this can make code less readable
function_two(function_one(a))
4.3 Using the type() function
As mentioned python uses dynamic typing. The type() function can output the datatype assigned to a variable
4.4 Using Jupyter Notebooks
Python programs (.py files) are essential in data analysis and structured software development. In this course, we will primarily use Jupyter Notebooks for coding. Jupyter Notebooks enable literate programming by merging natural language (like English) with source code in a single document. Instead of separate text documents and .py files, Jupyter Notebooks integrate both functionalities, making analyses and code reproducible. This is important for FAIR (Findable, Accessible, Interoperable, and Reproducible) research standards.
The name “Jupyter” derives from its core languages: Julia, Python, and R, with its logo inspired by Galileo’s discovery of Jupiter’s moons. Each coding language has its own kernel for executing code cells. For Python, the kernel is IPython. Jupyter Notebook files have a ‘.ipynb’ extension, indicating their connection to IPython.
The Jupyter Notebook file, kernel, and web browser interact via the Jupyter Notebook Server, enabling a seamless user experience.
For a more in-depth look at Jupyter Notebooks, refer to the MCB course: https://ac812.github.io/mcb-python/jupyter.html
4.5 Summary
You should now be able to call in-built functions in python and be set up to use Jupyter Notebooks.
- Python uses # for comments
- Variables are assigned with
=
- There is no need to define the variable type explicitly
- Indentation is used in python to identify code chunks
- Jupyter Notebooks enable literate programming in python, combining code chunks and markdown text