Introduction to Python¶


Course Details

This notebook provides a gentle introduction to the fundamentals building blocks of the Python programming language.

Course Website

https://maic.school/coding/python/

Requirements

  • Desktop or laptop computer (Windows, Mac or Linux).
  • Internet browser (for example Google Chrome).
  • Stable internet connection if using Google Colaboratory (recommended).
  • Google account if using Google Colaboratory (recommended).

Contents

  1. What is Python?
  2. Basic Data Types.
  3. Basic Operators.
  4. Basic Control Flow.
  5. Basic Functions.

Outcomes

  • The ability to develop simple software applications in Python 3.
  • An understanding of basic computing concepts including basic data types, control flow and functions.


1. What is Python?¶

Python is an open-source general purpose programming language. This means that it can be used to develop software for a wide variety of tasks. Today Python is used to create and maintain a huge range of computer applications and services including in relation to web applications, cyber security, hacking (both ethical and non-ethical), performing data analysis and media processing, robotics and developing artificial intelligence (AI) systems. Python is a popular choice for those wishing to learn computer programming and computer science fundamentals for the very first time because it is easy to learn, intuitive and is supported by an active global community of software engineers, data scientists and academics.

2. Basic Data Types¶

2.1. Strings¶
In [2]:
# String / text
my_first_string = 'Hello world!'
print(my_first_string)
Hello world!
2.2. Numbers¶
In [3]:
# Numbers
my_first_number = 10
my_second_number = 3.14159
2.3. Booleans¶
In [4]:
# Booleans
my_first_boolean = True
my_second_boolean = False
2.4. Naming Rules¶

In Python, the following rules must be adhered to when choosing names for your variables:

  1. Variable names can only include letters, numbers and the underscore _ character.
  2. Variable names cannot start with a number.
2.5. Naming Conventions¶

There also exist conventions which, though they do not need to be adhered to strictly unlike rules, are highly recommended to follow when choosing names for your variables:

  1. Variable names should be all lowercase (variable names are case-sensitive in Python).
  2. Different words in variable names should be separated by the underscore _ character.
  3. Variable names should be short and meaningful.
In [5]:
# Examples of compliant and good variable names
first_name = 'Jillur'
last_name = 'Quddus'
age = 30
male = True

print(f'{first_name} {last_name} is {age} years old.')
Jillur Quddus is 30 years old.

3. Basic operators¶

3.1. Arithmetic Operators¶
In [6]:
# Arithmetic operators
print(3 + 7)
print(10 - 6)
print(2 * 3)
print(10 / 2)
10
4
6
5.0
In [7]:
x = 2
y = 10
z = x * y
print(z)
20
3.2. Comparison Operators¶
In [8]:
# Comparison operators
print(10 == 100)
print(15 != 20)
print(2 > 10)
print(10 < 100)
print(10 >= 10)
print(20 <= 19)
False
True
False
True
True
False

4. Basic Control Flow¶

4.1. Conditional Statements¶
In [9]:
# If statement
age = 30
if age >= 18:
  print("You are allowed to vote in UK elections.")
You are allowed to vote in UK elections.
In [10]:
if age >= 18:
  print("You are allowed to vote in UK elections.")
else:
  print("You are NOT allowed to vote in UK elections.")
You are allowed to vote in UK elections.

5. Basic Functions¶

5.1. Print Function¶
In [11]:
# Print function
name = 'Jillur Quddus'
print(name)
Jillur Quddus
5.2. Custom Functions¶
In [12]:
# Defining your own functions
def add(x, y):
  sum = x + y
  return sum
In [13]:
# Running your own functions
add(10, 20)
Out[13]:
30