Introduction To Programming With Python
Introduction To Programming With Python
February 1, 2023
1 Contents
1.0.1 1. Introduction to Computers and Programming
1.0.2 2. Input, Processing, and Output
1.0.3 3. Decision Structures and Boolean Logic
1.0.4 4. Repetition Structures
1.0.5 5. Functions
1.0.6 6. Files and Exceptions
1.0.7 7. Lists and Tuples
1.0.8 8. More About Strings
1.0.9 9. Dictionaries and Sets
1.0.10 10. Classes and Object-Oriented Programming
1.0.11 11. Inheritance
1.0.12 12. Recursion
1.0.13 13. GUI Programming
1.0.14 14. Database Programming
2 INTRODUCTION
2.1 Different Ways of Using a Computer
2.2 In School
1. Writing papers
2.3 At work
1. Making presentations
1
2.4 At Home
1. Shopping online
2. Playing games
2.4.1 N ote :
The uses of computers are almost limitless in our everyday lives.
Computers can perform such a wide variety of tasks because they can be programmed
A program is a set of instructions that a computer follows to perform a task and they
are commonly referred to as sof tware .
Sof tware
1. System Sof tware programs that control and manage the basic operations of a computer.
Software System typically include the following types
a) Operating Systems
b) Utility Programs
2. Application Sof tware are programs that make a computer useful for everyday tasks.
2
2.9 Compilers and Interpreters
[1]: # and continue finally is raise
# as def for lambda return
# assert del from None True
# async elif global nonlocal try
# await else if not while
# break except import or with
# class False in pass yield
A compiler is a program that translates a high-level language program into a separate machine
language program.
The Python language uses an interpreter, which is a program that both translates and executes the
instructions in a high-level language program. As the interpreter reads each individual instruction
in the program, it converts it to machine language instructions then immediately executes them.
Hello world
Write a python program (output.py) using the print function to display your address.
3
Godfred Badu
Academic City University Colloge
P.O.Box AD 421
Haatso Accra
Ghana
Godfred Badu
Academic City University Colloge
P.O.Box AD 421
Haatso, Accra
Ghana
Kate Austen
123 Full Circle Drive
Asheville, NC 28899
2.14 Variables
A variable is a name that represents a value in the computer’s memory.
4
[9]: age = 25
age_carl = 19
AgeCarl = 20
[ ]:
[11]: print(width)
10
[12]: print(length)
I am staying in room number five hundred and three on the first floor
top_speed = 160
distance = 300
5
room= 503
dollars = 2.75
print('I have', dollars, 'in my account.')
99
[19]: x
N ote : We use Python’s built-in input function to read input from the keyboard.
variable = input(prompt)
[20]: name = input("what is your name? ")
6
[21]: print(name)
Badu Godfred
12 + 2
hours * pay_rate
7
[ ]: # Assign a value to a salary variable
salary = 2500.00
[ ]: 5/2
[ ]: 5 // 2
8
[ ]: -5//2
9
# Get the number of remaining seconds
seconds = total_seconds % 60
F
P = (1)
(1 + r)n
10
[4]: var1 = 2
var2 = 3
var3 = 4
var4 = 1/2
31.5
Python also allows you to break any part of a statement that is enclosed in parentheses into multiple
lines without using the line continuation character. For example, look at the following statement:
[5]: print("Monday's sales are", "monday",
"and Tuesday's sales are", "Tuesday",
"and Wednesday's sales are", "Wednesday")
Monday's sales are monday and Tuesday's sales are Tuesday and Wednesday's sales
are Wednesday
Hello World
Hello World
Godfred Badu
Academic City
P.O.Box AD 421
[5]: print('Mon\tTues\tWed')
print('Thur\tFri\tSat')
11
Mon Tues Wed
Thur Fri Sat
Hello world
[12]: temperature = 45
print(f'It is currently {temperature} degrees.')
It is currently 45 degrees.
5.0
[ ]: val = 10
print(f'The value is {val + 2}.')
12
third_number = int(input("Please enter your third_number: "))
[ ]:
[ ]:
13