0% found this document useful (0 votes)
20 views41 pages

Python Notes

The document provides an introduction to Python programming, explaining fundamental concepts such as programming languages, basic mathematical operations, and various operators including augmented assignment, comparison, and logical operators. It also covers digital logic circuits, operator precedence, variable naming rules, and types of programming errors. Additionally, it includes examples and assignments for practical application of the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views41 pages

Python Notes

The document provides an introduction to Python programming, explaining fundamental concepts such as programming languages, basic mathematical operations, and various operators including augmented assignment, comparison, and logical operators. It also covers digital logic circuits, operator precedence, variable naming rules, and types of programming errors. Additionally, it includes examples and assignments for practical application of the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to Python

Programming
Elliot Attipoe

1
What is a programming language?
1. A programming language is a set of rules
that provides a way of telling a computer
what operations to perform.
2. A programming language is a vocabulary
and set of grammatical rules for instructing
a computer or computing device to perform
specific tasks.
3. A programming language is a set of
commands, instructions, and other syntax use
to create a software program. 2
3
Top Ranking Programming Languages

4
About Python Programming Lang
• Python is a widely used general-
purpose, high level programming
language.

• It was initially designed by Guido van


Rossum in 1991

5
6
7
8
Basic Mathematics Operations in
Python
Operator Name
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor/Integer Division
9
Evaluate the following expressions
1. 3*1**3

2. 2 + 3 * 5 / 2

3. 2 + 3 * 5 // 2

4. 10 – 4 * 2

5. (10-4) * 2

6. 6 + 3 – 5 * 4 / 2 % 2

10
Augmented Assignment Operators
Operator Name
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Float division assignment
//= Integer division assignment
%= Remainder assignment
**= Exponent assignment
11
Assume i = 1, what are the results
of the following expressions?
1. i+=4
2. i-=4
3. i*=4
4. i/=4
5. i//=4
6. i%=4

12
Comparison Operators
Operator Operator Name
== Equal to
!= Not Equal to
> Greater Than
< Lesser Than
>= Greater Than or Equal to
<= Lesser Than or Equal to
13
What is the output of the following
expressions
1.10 == 12

2.10 != 12

3.10 < 12

4.10 > 12

5.10 <= 12
14
Logical Operators
Operator Operator Name
and Logical AND
or Logical OR
not Logical NOT

15
Evaluate the following expressions
1. True and False
2. True or False
3. not(True) and False
4. not(True and False)
5. (10 < 0) and (10 > 2)
6. (10 < 0) or (10 > 2)
7. not(10 < 0) or (10 > 2)
8. not(10 < 0 or 10 > 2)

16
17
18
19
20
21
22
Digital Logic Circuit
• Digital circuits make up all computers and computer
systems.
• The operation of digital circuits is based on Boolean
algebra Boolean Logic (AND, OR, and NOT)
• These basic functions can be combined in many ways
to provide all the functions required in the central
processor of a digital computer a digital computer.
• Digital circuits operate by performing Boolean
operations on binary numbers

23
Precedence and Associativity
• Operator precedence determines the way in
which operators are parsed with respect to
each other.
• Associativity determines the way in which
operators of the same precedence are
parsed.
• Almost all the operators have left-to-right
associativity.

24
Operator Precedence in Python
Operator Meaning
() Parentheses
** Exponent
*, /, //, % Multiplication, Division, Floor division,
Modulus
=, - Addition, Subtraction
==, !=, >, >=, <, <= Comparison
not Logical NOT
and Logical AND
or Logical OR
25
Evaluate the following expression

a) 13 > 12 or 6 < 5

b) 6 + 3 > 7 and 11 < 2 * 5

c) 7 + 3 * 2 > 6 * 3 and True

d) 5 * 4 > 6 ** 2 and True or False

26
Variables
• A variable is a storage location (identified by
a memory location) which contains some
known or unknown quantity of information
referred to as value.

• Programmers get to choose the names of the


variables

27
Python Variable Name Rules
• Can start with a letter or underscore _
• Can consist of letters and numbers and underscore
• Is Case Sensitive

• Good examples
• name, age tom25, _max
• Bad examples
• 45name, #name, age.22
• Different: name, Name, NAME (case sensitive)
28
Reserved words in Python

29
Programming Errors

• Programming errors can be categorized into


three types:
• syntax errors
• runtime errors
• logic errors

30
Syntax Errors
• Syntax errors result from errors in code
construction, such as mistyping a statement,
incorrect indentation, omitting some necessary
punctuation, or using an opening parenthesis
without a corresponding closing parenthesis.

31
Runtime Errors
• Runtime errors are errors that cause a
program to terminate abnormally.
• They occur while a program is running if the
Python interpreter detects an operation that is
impossible to carry out.
• Input mistakes typically cause runtime errors.

32
Logic Errors
• Logic errors occur when a program does not
perform the way it was intended to.

• Errors of this kind occur for many different


reasons.

33
Question
• Write a program to compute the volume
of a sphere.

34
Assignment
• Write a program to compute the roots of a
quadratic equation.

Submit source code via the e-learning platform


Due Date: May 2, 2022 @ 6pm

35
IF…Else statement

36
If…Else Example

37
While Loop

38
While Loop Example

39
For…Loop
1.for i in range (5):
2. print ("Hello")

• for i in range (5):


• print (i+1)
40
Assignment
• Use the turtle module in Python to draw the
following:
a) The Olympics rings logo
b) The Ghana Flag

• Submit source code via the e-learning


platform
• Due Date: May 2, 2022 @ 6pm
41

You might also like