0% found this document useful (0 votes)
21 views39 pages

Topic 3

This document provides an introduction to programming with Python, covering its basic structure, syntax, and the ability to write simple scripts. It explains the nature of programming languages, differentiates between users and programmers, and highlights the features and advantages of Python. Additionally, it discusses the interactive and script modes of Python, basic data types, and fundamental programming concepts such as input and output.

Uploaded by

kielderjohn524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views39 pages

Topic 3

This document provides an introduction to programming with Python, covering its basic structure, syntax, and the ability to write simple scripts. It explains the nature of programming languages, differentiates between users and programmers, and highlights the features and advantages of Python. Additionally, it discusses the interactive and script modes of Python, basic data types, and fundamental programming concepts such as input and output.

Uploaded by

kielderjohn524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Topic 3 – Introduction to Programming

With Python

Python Programming
CT108-3-1-PYP
TOPIC LEARNING OUTCOMES

At the end of this topic, you should be able to:


• Understand the basic structure and syntax of Python programming
language
• Write your own simple Python scripts

CT108-3-1-PYP Introduction to Programming With Python SLIDE 2


Contents & Structure

• Introduction to Python
• Python as a programming language
• Interactive vs script programming

CT108-3-1-PYP Introduction to Programming With Python SLIDE 3


What is programming language

• Is a set of rules that provides a way of telling a computer what


operations to perform.
• Is a set of rules for communicating an algorithm
• It provides a linguistic framework for describing computations
• Is a notational system for describing computation in a machine-
readable and human-readable form.
• Is a tool for developing executable models for a class of problem
domains.

CT108-3-1-PYP Introduction to Programming With Python SLIDE 4


Programming Languages

• Some influential ones:


– FORTRAN
• science / engineering

– COBOL
• business data

– LISP
• logic and AI

– BASIC
• a simple language

CT108-3-1-PYP Introduction to Programming With Python SLIDE 5 5


Programming Languages

There are hundreds of different programming


languages.

A few basic types of instructions appear in all of


them
• Input (get input from the user)
• Output (display data to the user on a monitor or printout)
• Math (perform basic mathematical operations)
• Conditional statement (check for conditions and execute the
appropriate sequence of statements)
• Repetition (perform some sequences of statements repeatedly)

CT108-3-1-PYP Introduction to Programming With Python SLIDE 6 ‹#›


Users .vs. Programmers

Which are you?

Which do you want to be?


Do you ever wonder how
does it work?
CT108-3-1-PYP Introduction to Programming With Python SLIDE 7
‹#›
Users vs. Programmers

Users

• Literally someone who uses the computer or the smart-phone or the e-reader.
• Often is not an expert; he or she just owns the device and knows how it works.
• Example: Ali is an educated person, but computers are not his area of expertise
• See computers as a set of tools - word processor, spreadsheet, map, todo list, etc.

Programmers

• Someone who understands computers from a technological point of view.


• Usually writes the code (the special language necessary to make the computer
understand commands and perform various functions)
• Earn the computer “ways” and the computer language
• Have tools to build new tools
• Write tools to automate a task

CT108-3-1-PYP Introduction to Programming With Python SLIDE 8


‹#›
What is a Program/Code/Software?

A computer cannot solve or do any task on its own. You must


instruct it to do what you want it to do

A Program is a set of It is a little piece of our intelligence in the computer


It is a little piece of our intelligence we can give to
instructions to the Computer to others - we figure something out and then we encode it
and then give it to someone else to save them the time
perform a job/task. and energy of figuring it out

A piece of creative art - particularly when we do a good job on


user experience

CT108-3-1-PYP Introduction to Programming With Python SLIDE 9


‹#›
Programming basics

• code or source code: The sequence of instructions in a program.

• syntax: The set of legal structures and commands that can be used
in a particular programming language.
• output: The messages printed to the user by a program.

• console: The text box onto which output is printed.


– Some source code editors pop up the console as an external window, and others
contain their own console window.

CT108-3-1-PYP Introduction to Programming With Python


10 SLIDE 10
Learning Python

Learning a new programming language like learning a new


human language like English

First step would be to learn the language elements: symbols,


words, sentences, grammar

For a programming language: symbols, variables, constants,


statements, control structures

CT108-3-1-PYP Introduction to Programming With Python SLIDE 11


‹#›
What is Python?

Python is a widely used programming language

First implemented in 1989 by Guido van Rossum

Free, open-source software with community-based


development

Trivia:

• Python is named after the BBC show “Monty Python’s Flying Circus” and
has nothing to do with reptiles

CT108-3-1-PYP Introduction to Programming With Python SLIDE 12 ‹#›


Which Python

There are 2 widely used Python2.7 and


versions of Python: Python3.x

We will use Python3

Many help forums still refer to Python2, so make sure you are
aware which version is being referenced

CT108-3-1-PYP Introduction to Programming With Python SLIDE 13 ‹#›


Why Python?

Python is Compared to:

• easy to learn, • C is much faster but


• relatively fast, much harder to use.
• Java is about as fast
• object-oriented, and slightly harder to
• strongly typed, use.
• widely used, and • Perl is slower, is as
• portable. easy to use, but is not
strongly typed.

CT108-3-1-PYP Introduction to Programming With Python SLIDE 14


‹#›
Why Python? - Continued

Object-oriented
• Structure supports such concepts as polymorphism, operation overloading, and multiple inheritance.

Indentation
• Is one of the greatest future in Python.

It's free (open source)


• Downloading and installing Python is free and easy
• Source code is easily accessible

It's powerful
• Dynamic typing
• Built-in types and tools
• Library utilities
• Third party utilities (e.g. Numeric, NumPy, SciPy)
• Automatic memory management

It's portable
• Python runs virtually every major platform used today
• As long as you have a compatible Python interpreter installed, Python programs will run in the same manner, irrespective of platform.

CT108-3-1-PYP Introduction to Programming With Python SLIDE 15 ‹#›


Why Python? - Continued

It's mixable

• Python can be linked to components written in other languages easily


• Linking to fast, compiled code is useful to computationally intensive problems
• Python/C integration is quite common

It's easy to use

• No intermediate compile and link steps as in C/ C++


• Python programs are compiled automatically to an intermediate form called bytecode, which the
interpreter then reads
• This gives Python the development speed of an interpreter without the performance loss inherent in
purely interpreted languages

It's easy to learn

• Structure and syntax are intuitive and easy to grasp

CT108-3-1-PYP Introduction to Programming With Python SLIDE 16 ‹#›


What can I do with Python?

Graphical
System User Internet
programming Interface Scripting
Programming

Gaming,
Component Database Images,
Integration Programming XML , Robot
and more
CT108-3-1-PYP Introduction to Programming With Python SLIDE 17 ‹#›
Compiling and Interpreting

• Many languages require you to compile (translate) your program into


a form that the machine understands.

• Python is instead directly interpreted into machine instructions.

CT108-3-1-PYP Introduction to Programming With Python SLIDE 18


‹#›
Your First Python Program – Python IDLE

>>> 1 + 2
3
>>> 2 * 3
6
>>> x = 1
>>> print(x)
1
>>> x = x + 1
>>> print(x)
2
>>> exit()

This is a good test to make sure that you have Python correctly installed. Note that quit()
also works to end the interactive session.

CT108-3-1-PYP Introduction to Programming With Python SLIDE 19


‹#›
Understand the Code

Indentation matters to code meaning

• Block structure indicated by indentation

First assignment to a variable creates it

• Variable types don’t need to be declared.


• Python figures out the variable types on its own.

Assignment is = and comparison is ==

For numbers + - * / % are as expected

• Special use of + for string concatenation and % for string formatting (as in C’s printf)

Logical operators are words (and, or, not) not symbols

The basic printing command is print

CT108-3-1-PYP Introduction to Programming With Python SLIDE 20 ‹#›


Sentences or Lines

x = x + 2
x = 2 print(x)
Assignment
Assignment Print
with
Statement statement
expression

CT108-3-1-PYP Introduction to Programming With Python SLIDE 21


‹#›
Basic Datatypes

Integers (default for numbers)

z = 5 / 2 # Answer 2, integer division

Floats

x = 3.456

Strings

• Can use “” or ‘’ to specify with “abc” == ‘abc’


• Unmatched can occur within the string: “matt’s”
• Use triple double-quotes for multi-line strings or strings than contain both ‘ and “
inside of them:
“““a‘b“c”””

CT108-3-1-PYP Introduction to Programming With Python SLIDE 22


‹#›
Whitespace

Whitespace is meaningful in Python: especially indentation and


placement of newlines

Use a newline to end a line of code Use \n when must go to next line prematurely

No braces {} to mark blocks of


First line with less indentation is outside of the block
code, use consistent indentation First line with more indentation starts a nested block
instead

Colons start of a new block in many constructs, e.g., function definitions,


then clauses

CT108-3-1-PYP Introduction to Programming With Python SLIDE 23


‹#›
Commenting in Python

Useful when your code needs further explanation. Either for your future self and
anybody else.
Useful when you want to remove the code from execution but not permanently

Comments in Python are done with #

Describe what is going to happen in a sequence of code

Document who wrote the code or other ancillary information

Turn off a line of code - perhaps temporarily

CT108-3-1-PYP Introduction to Programming With Python SLIDE 24


‹#›
Python Scripts

When you call a python program from the command line the interpreter
evaluates each expression in the file

Familiar mechanisms are used to provide command line arguments and/or


redirect input and output

Python also has mechanisms to allow a python program to act both as a


script and as a module to be imported and used by another python
program

As convention, we add “.py” as the suffix on the end of these files to


indicate they contain Python

CT108-3-1-PYP Introduction to Programming With Python SLIDE 25


‹#›
Interacting with Python

• There are 2 main ways of interacting with Python:


Interactive Mode Manual Mode
Descriptio Takes single user inputs, evaluates them Execute a Python
n and returns the result to the user (read- script on the Unix
eval-print-loop (REPL)) command prompt
Benefits • Use as a sandbox: explore new • Run long
features complicated
• Easy to write quick “throw away” programs
script • The script
• Useful for debugging contains all the
• Use it as a calculator commands
Usage Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 <script.py>
This is Python’s command prompt. It means,
2022, 16:14:13) [MSC v.1929 64 bit (AMD64)]
on win32 “I’m ready for a command!” Do not type the
“>>>” "credits" or
Type "help", "copyright",
"license()" for more information.
>>>

CT108-3-1-PYP Introduction to Programming With Python SLIDE 26


‹#›
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

CT108-3-1-PYP Introduction to Programming With Python SLIDE 27


‹#›
Print

• When writing scripts, your outcomes aren't printed on the terminal.


• Thus, you must print them yourself with the print() function.
• Beware to not mix up the different type of variables!

CT108-3-1-PYP Introduction to Programming With Python SLIDE 28


‹#›
Print

print : Produces text output on the console.

Syntax:

print("Message“)
print Expression
• Prints the given text message or expression value on the console and moves the cursor down to the next line.
print(Item1, Item2, ..., ItemN)
• Prints several messages and/or expressions on the same line.

Examples:

print("Hello, world!“)
age = 45
print("You have", 65 - age, "years until retirement“)

Output:

Hello, world!
You have 20 years until retirement

CT108-3-1-PYP Introduction to Programming With Python SLIDE 29


‹#›
print Statement - Example

• Elements separated by commas print with a space between them


• A comma at the end of the statement (print ‘hello’,) will not print a
newline character

>>>print(‘hello’)
hello
>>>print(‘hello’ + ‘there’)
hellothere

CT108-3-1-PYP Introduction to Programming With Python SLIDE 30


‹#›
input

• input : Reads a number from user input.


– You can assign (store) the result of input into a variable.
– Example:
age = int(input("How old are you? "))
print("Your age is", age)
Print("You have", 65 - age, "years until retirement”)
– Output:
• How old are you? 53
• Your age is 53
• You have 12 years until retirement

CT108-3-1-PYP Introduction to Programming With Python SLIDE 31


‹#›
input Statement - Example

print("What's your name?")


name = input("> ")
print("What year were you born?")
birthyear = int(input("> "))
print("Hi ", name, "!", "You are ", 2022 - birthyear)

CT108-3-1-PYP Introduction to Programming With Python SLIDE 32


‹#›
Program Steps or Program Flow - Example

Pizza Dough Recipe


1. Gather Ingredients
2. Combine sugar (1tbs), salt (1tbs), olive oil (1tbs), flour (1c) in mixing
bowl Variable
3. Turn on mixer Sequence of
4. Add 1/4 cup of flour statements
5. If dough comes off the sides go to step 6, otherwise go back to step 4
6. Knead 15 minutes Conditional
7. Let rest for at least 45 minutes in warm area

CT108-3-1-PYP Introduction to Programming With Python SLIDE 33


‹#›
Sequential Steps or Flow - Example
Start

x =2 Output:

2
4
print x

x = x +2
When a program is running, it flows from one step
to the next. We as programmers set up “paths”
print x for the program to follow

End
‹#›

CT108-3-1-PYP Introduction to Programming With Python SLIDE 34


Conditional Steps or Flow - Example
START

Output:
x=5
Smaller
Finish
Yes print
x < 10?
‘Smaller’

No
Yes print
x > 20?
‘Bigger’

No
print
‘Finish’

CT108-3-1-PYP Introduction to Programming With Python SLIDE 35


‹#›
Repetition Steps or Flow - Example
START
Loops (repeated steps) have iteration variables
that change each time through a loop. Often
n=5 these iteration variables go through a sequence of
numbers.

n > 0? No print
‘Blastoff’

Yes Output:
5
print ‘n’ 4
3
2
n = n-1 1
Blastoff!

CT108-3-1-PYP Introduction to Programming With Python SLIDE 36


‹#›
Summary / Recap of Main Points

• Introduction to Python
• Python as a programming language
• Interactive vs script programming

CT108-3-1-PYP Introduction to Programming With Python SLIDE 37


What To Expect Next Week

In Class Preparation for Class


• Variables and Data Types • Students are required to
– Identify data type involved in
Python

CT108-3-1-PYP Introduction to Programming With Python SLIDE 38


CT108-3-1-PYP Introduction to Programming With Python SLIDE 39

You might also like