CHAPTER 1:
Python Review
Python Syntax
● Python is a dynamically-typed language i.e. a variable does not need to be
declared with a type and one variable can take on different data types.
● Python code is written and executed line by line. We typically separate
different instructions with lines but semicolons are also valid.
● It uses indentation instead of curly brackets for code blocks.
● Python promotes code readability and ease of understanding
Lists and tuples
● List
○ An ordered, mutable collection of elements (the list’s structure can be
modified after it has been created)
○ Has methods for adding and removing elements (append, extend, pop,
etc.)
● Tuples
○ An immutable ordered collection of elements (cannot be modified after
creation, to put it simply)
Sets and dictionaries
● Sets
○ An unordered collection of unique elements
○ Can be created from other iterables like lists, strings and tuples
● Dictionaries
○ Store key-value pairs. The key can only be hashable data types like
numbers, strings or tuples.
○ The values can be any Python object
Conditionals
● Conditional statements control the flow of execution
Loops
● Python offers for loops and while loops. You can also add an else statement to
the end of a loop.
● Both loops support break and continue.
For loop While loop
Functions
● Functions allow code
reusability
● Python functions can accept 0
or more arguments and even
permit the use of placeholders
(*args, **kwargs)
● Functions can also return
multiple values
Django
Installation
and Setup
Django Installation
● Before getting started, please create a new folder on your computer for this
course titled Advanced_OOP. Please take note of where this folder is located.
● Now open a terminal and type the pip command to install django: pip install
django or python -m pip install django
● Once installation has successfully completed, check the version: django-admin
--version
● If the above check doesn’t work, try
○ running python -m django-admin --version instead
○ opening a python shell and importing django
Creating a Django project
● After verifying that Django has been successfully installed, create a new folder
in the Advanced_OOP folder called Exercises (please respect the casing)
● Open a terminal in the Exercises folder created above. We are going to create
our first django project here
● On the terminal, type django-admin startproject example
● Navigate into the example project: cd example
● Run the development server: python manage.py runserver
● Navigate to http://localhost:8000 on your browser.
Exercises
● Create a list of numbers from 1 to 10 and print only even numbers.
● Write a function that takes a name as input and returns "Hello, !".
● Write a program that asks for a user's age and prints if they are a minor or an
adult.
● Write a while loop that prints numbers from 5 to 0.
● Use the `math` module to compute the square root of a number entered by
the user.
● Use a regular expression to extract all email addresses from a given text.