Introduction to ArtificiaI Intelligence in Higher Education
Python Lecture 1
1. Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
BS GIS Instructor: Inzamam Baig
Lecture 1
Fundamentals of Programming
2. Why are you learning programming?
computers are good at things that humans are not
in order to tell computer how to do a certain task you need to
learn a “computer language”
computer makes our life easier
computers want to helpful but how?
3. Users vs. Programmers
Users(lay people) see computers as a set of tools - word
processor, spreadsheet, map, to-do list, etc.
Programmers learn the computer “ways” and the computer
language
Programmers have some tools that allow them to build new tools
Programmers sometimes write tools for lots of users
4. Why be a Programmer?
To get some task done - we are the user and programmer
To produce something for others to use - a programming job
5. What is Code? Software? A Program?
A sequence of stored instructions
- It is a little piece of our intelligence in the computer
- We figure something out and then we encode it and then give it to someone else
to save them the time and energy of figuring it out
A piece of creativity
6. Simple representation of computer
hardware
Software
Input
and Output
Devices
Central
Processing
Unit
Main
Memory
Secondary
Memory
7. Python
Created by Guido van Rossum and first released in 1991
Python is an interpreted, high-level, general-purpose
programming language.
An individual who can speak Python is known as a Pythonista.
8. Starting python as your first programming
language
In the beginning we will make lots of mistakes
When you make a mistake, the computer will yell “syntax error”
The computer is simple and very fast, but cannot do things by its
own
9. Your first program
C:UsersTestDesktop>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019,
23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for
more information.
>>>print(‘Hello World’)
10. Python Scripts
Interactive Python is good for experiments and programs of 3-4
lines long.
Most programs are much longer, so we type them into a file and
tell Python to run the commands in the file.
In a sense, we are “giving Python a script”.
As a convention, we add “.py” as the suffix on the end of these
files to indicate they contain Python.
11. Interactive versus Script
Interactive
- You type directly to Python one line at a time and it responds
Script
- You enter a sequence of statements (lines) into a file using a text
editor and tell Python to execute the statements in the file
12. Re Writing first program as a Script
Open your favorite text and type:
print(‘hello world from a script’)
Save file as file_name.py
Open command prompt and change directory to the where you saved
the python file
Type:
python file_name.py
13. Interpreter vs. Compiler
Python is a high-level language.
The actual hardware inside the Central Processing Unit (CPU)
does not understand any of these high-level languages 1
The CPU understands a language we call machine language2
14. Interpreted vs. Compiled
An interpreter reads the source code of the program as written by
the programmer, parses the source code, and interprets the
instructions on the fly.
Python is an interpreter1
15. Interpreted vs. Compiled
A compiler needs to be handed the entire program in a file, and
then it runs a process to translate the high-level source code into
machine language and then the compiler puts the resulting
machine language into a file for later execution.
16. Python Interpreter
The Python interpreter is written in a high-level language called
“C”.
So Python is a program itself and it is compiled into machine
code
In Windows, the executable machine code for Python itself is
likely in a file with a name like:
C:Pythonpython.exe
17. Program in a nutshell
Like a recipe or installation instructions, a program is a sequence
of steps to be done in order.
Some steps are conditional - they may be skipped.
Sometimes a step or group of steps is to be repeated.
Sometimes we store a set of steps to be used over and over as
needed several places throughout the program.
18. Sequential Steps
x = 2
print(x)
x = x + 2
print(x)
Program:
x = 2
print(x)
x = x + 2
print(x)
Output:
2
4
19. Conditional Steps
Output:
x is smaller
End
Program:
x = 5
if x < 10:
print(' x is smaller ')
if x > 20:
print(' x is bigger ')
print(‘End')
x = 5
x < 10 ?
print(‘x is smaller')
x > 20 ?
print(‘x is bigger')
print(‘End')
Yes
No
No
Yes
20. Repeated Steps
Output:
5
4
3
2
1
Blastoff!
Program:
n = 5
while n > 0 :
print(n)
n = n – 1
print('Blastoff!')
n > 0 ?
Loops (repeated steps) have iteration variables that change each time through
a loop.
No
print('Blastoff')
Yes
n = 5
print(n)
n = n -1