You are on page 1of 29

PYTHON 3

PROGRAMMING
Prepared by
Akintola Victor (ICT Instructor AFGMS)
PYTHON
Prepared by
Akintola Victor (ICT Instructor AFGMS)
THE WAY OF THE PROGRAM
Thinking like a programmer combines some of the feature of maths,
engineering, and natural science, programmers use formal
language(programming syntax) to denote ideas.
The Single most important skills for a programmer is problem
solving! It means, the intellectual capacity to formulate problems,
think creatively about the solution, and express a solution clearly and
accurately.
A program is a sequence of instructions that specifies how to
perform a computation, it may be mathematical, symbolic, or
graphical. Programming languages are used to express computations.
Computation’s usually entails : input – output – mathematical –
conditional execution, repetition.
PYTHON
Python is an High Level Language created by Van Don Rossum, it is
characterized by its clear and concise code.
Its Applications include Artificial Intelligence (AI), 3D (CAD ,AR, VR),
Desktop Applications, Web Development, Malware Development,
Mobile App Development, among others.
To use python programming language you have to download a
version which is about 25mb from www.python.org. after download
install the software to get started.
To check if python is successfully installed on the OS, open command
prompt (CMD) and type python and a SHELL would be instantiated.
The SHELL is a program that reads and execute python code.
YOUR FIRST PROGRAM
There are three ways to start writing python code; using the CMD,
using the IDLE, and using an IDE (Integrated Development Env.).
print(“Hello World my name is xavier”)

The print() function is used to display their content(argument) to the


standard output.
IDLE is the pre-installed application for python development,
complete with the IDLE SHELL, Documentation, and Text-editor.
Press F1 to open the Documentation which contains everything
about Python programming.
It is important to note that the SHELL is interactive in nature.
To write a script: File – New which instantiate the Text Editor.
VALUES AND VARIABLES
A value is one of the basic thing a program works with including
letters, number (integers), or booleans eg “A”, “Hello”, 45, 45.2, True .
Value have a data type, can be crosschecked using the type() method
type(“Hello World)

Data types are generally classified as numeric, string, and Boolean.


Variables are used to store values, the assignment operator = is used
to assign a value to a variable
name = “Xavier”
age = 40
print(“print my name is ”, name, “am aged “, age)

Variables can be used subsequently in computation .


STRING TEMPLATES
Strings are are letters or anything enclosed within quotation marks.
A string value is a sequence of characters in double quotes.
String template provides a clean method to display output with the
mixture of values and or variables.
String template expressions are used to access data stored in
variables and other objects, and convert them into strings
A letter f is appended with strings to make it a template.
The squiggly braces in the string template serves as a placeholder for
values and variables.
name = “Xavier”
age = 40
print(f“My name is {name}, am aged {age}”)
INPUT() FUNCTION
1. The input() function is used to read or collect user input, the value
can be subsequently used in computation.
# 1. A simple program to read user name and print a greeting
name = input(“Enter your name: ”)
time = input(“Enter time of the day: ”)

printf(f”good {time} you are welcome {name})”

#2. A simple program to add numbers


# type cast input to integers
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

sums = num1 + num2


print(f"The sum of {num1} and {num2} is {sums}")
LIKELY QUESTIONS – Module 1
What is a formal language, problem solving,
What is a program, programming language.
Computation’s usually entails which components
Creator of python and in what year.
Applications of Python programming language
Ways to write python code
What is SHELL, IDE, CMD, OS, CAD, AI, VR, AR.
What is a Value, Variable, Data type.
What is the print() function/method used for?
What is the type() function/method used for ?
BASIC DATATYPES
Prepared by
Akintola Victor (ICT Instructor AFGMS)
For
Emmakulate Systems Solutions
DATA TYPES
Data types can also be reffered to as “Literals” which represent
specific kinds of information. (see Pg. 24 Python Handbook).
Knowing the specific types of data is very important because its
informs you of the operations you can perform with that type.
Python has 3 basic data types which are; Numerical, Strings
(Alphabeths), and Boolean (True/False)
The type() method is used to check the data type of a value or
variable
The isinstance() method can also be used to check data types.
type(40) #int
type(“adam”) #str
type(True) #bool
isinstance(40, int) #True
NUMERICAL LITERALS
Numerical Literals represent number types.
There are 3 types of numerical literals: int, float, complex
integer = 100
floats = 60.5
compl = 3j
Integers represent whole numbers, floats represent numbers with
points, while complex numbers have a real and imaginary part.
Types of numerical integer literals are : binary (base 2), octal(base 8),
decimal (base 10), hexadecimal (base 16). (see Pg. 86 Python for Dummies).
# number base conversions
binary = bin(integer)
octal = oct(integer)
hexadecimal = hex(integer)
STRING LITERALS
Strings is a character or a sequence of characters in double quotes.
Every letter under the hood is represented by numbers you can use
the ord() method to derive the numerical representation of letters.
Strings also have a number of methods which can be used for string
manipulations (see Pg. 214 Python for Dummies).
string = “eve”
print(string.upper())
print(string.lower())
print(string.capitalize())
Parts of strings can be printed using indexes to slice the strings
string = “Python”
print(string[0 : 3]) #Pyt
BOOLEAN LITERALS
The Bool datatypes can only contain one of two values (True/False).
Booleans are intended to represent the truthfulness or falsehood of
logic, it is named after George boole who first defined an algebraic
system of logic in the 19th Century.
Booleans are the foundation of decision making in programming.
Booleans are represented by the data type bool, and can be used to
create expressions that equates to True or False
T = True
F = False
print(type(T))
print(type(T))
ASSIGNMENT 1
In less than five lines each less write a short note on the following
terms;
The concept of Logic
Logic Gates
Logic table – Truth Table
Boolean algebra
The type() method
OPERANDS, OPERATORS,

AND OPERATIONS
Prepared by
Akintola Victor (ICT Instructor AFGMS)
OPERATORS
operators
if , elif, else statements
ESCAPE SEQUENCES
2. There are different types of escape sequences including :

# newline \n escape sequence


print("God bless Nigeria\nGod bless NAF\nGod bless AFMS")

# horizontal tab
print("This is an horizontal \t tab")

# vertical tab
print("This is an horizontal \v tab")
NUMERICAL LITERALS
2. You can also perform arithmetic operations with numerical literals.
when a float is added to an integer you get floats
# arithmetic operations
int1 = 20
int2 = 40

print(f"Addition: {int1 + int2}")


print(f"Multiplication: {int1 * int2}")

# floats and int artithmetic operation


print(integer + floats)
NUMERICAL LITERALS
3. Types of numerical integer literal are : binary (base 2), octal(base 8), decimal
(base 10), hexadecimal (base 16).

# number base conversions


binary = bin(integer)
octal = oct(integer)
hexadecimal = hex(integer)

print(f"Binary: {binary}\nOctal: {octal}\nHexadecimal: {hexadecimal}")


BOOLEAN LITERALS
1. The Boolean Literals are made up of only two types True and False.
 Comparision/Relational operators are used to compare two operands and evaluate to a
boolean value
# comparison operators compares two or more operands and evaluate to a
boolean value
x = 10 # less than
y = 20 print(x < y)

# equals to # less than equal to


print(x == y) print(x <= y)

# greater than # not equal to


print(x > y) print(x != y)

# greater than equal to


print(x >= y)
BOOLEAN LITERALS II
2. The Boolean Literals are made up of only two types True and False.
 Logical Operators (and, or , not) can be used to combine multiple conditions

truthy = True
falsy = False

# logical operators and or not


# and logical operator : true if both operands are true
print(f"T and T: {truthy and truthy}")
print(f"F and F: {falsy and falsy}")
print(f"T and F: {truthy and falsy}")

# or logical operator: true if either operand is true


print(f"T or T: {truthy or truthy}")
print(f"F or F: {falsy or falsy}")
print(f"T or F: {truthy or falsy}")
BOOLEAN LITERALS III
3. The Boolean Literals are the foundation of decisin making in programming.
 The not Logical Operators are use to invert operands.
 relative and logical operators can be used in conjuction to combine various conditions.
# not logical operator: invert the operand
print(f"not T: {not truthy}")
print(f"not F: {not falsy}")

# using relative and logical operators to combine diferent conditions


print(x != y and x < y)
print(x != y or x < y)
FUNCTIONS
1. A function is a self contained program segment which carries out
some specific well defined task.
# 1. defining functions
def greet():
print("good morning")

# calling functions
greet()
FUNCTIONS
2. A function can be defined with parameters which are going to be
used in the computation.
Parameters are specified placeholders during function definations.
Arguements supply information to the function to use in processing.

# 2. defining functions with parameters


def greet_person(name):
# string interpolation output format
print(f"good morning {name}")

# defining a variable
person = "nenret"

# supplying person variable as arguement name to the function call


greet_person(person)
FUNCTIONS
3. A function can be defined with multiple parameters
# 3. defining functions with multiple parameters
def time_greet(name, time):
# string concatenation output format
print("good"+" "+time+" "+name)

# defining a variables used as arguments to the function call


person = "nenret"
time_of_the_day = "afternoon"

# supplying multiple arguements to the function call


time_greet(person, time_of_the_day)
FUNCTIONS
4. A function that adds two numbers
# 4. A function that adds two numbers
def add(num1, num2):
sum = num1 + num2
print(f"the sum of {num1} and {num2} is {sum}")

add(2,3)
FUNCTIONS
5. A function that calculates the manhattan distance between two
points : point1(lat, long), point2(lat, long).
# 5. A function that calculates manhattan distance between two points
# manhattan distance formula = (x2-x1) + (y2 - y1)

def distance(x1, x2, y1, y2):


formula = (x2-x1) + (y2-y1)
print(f"The manhattan distance is {formula}")

distance(2,5,3,6)
FUNCTIONS
5. A modified version of the function using array data structures for
simplifying computation.

# using array data structure to simplify computation


def dist_calc (point1, point2):
dist_formula = (point1[1] - point1[0]) + (point2[1] - point2[0])
print(f"The calculated distance is {dist_formula}")

# defining arrays
point1 = [2,5]
point2 = [3,6]

# call function using arrays point1 and point2 as arguements


dist_calc(point1, point2)

You might also like