29
Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Spring 2018 Some slides have been adapted from “Python Programming: An Introduction to Computer Science”

Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

  • Upload
    others

  • View
    49

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Fundamentals of Programming(Python)

Getting Started with Programming

Ali TaheriSharif University of Technology

Spring 2018

Some slides have been adapted from “Python Programming: An Introduction to Computer Science”

Page 2: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Outline1. Software Development Paradigm

2. Defining the Problem

3. Creating a Design

4. Coding with Python

5. Testing the Program

2ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 3: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Software Development

1. Define the Problem◦ Objectives?

◦ Inputs?

◦ Outputs?

◦ Process?

2. Create a Design◦ With algorithm & flowcharts

3. Code the Program◦ Using a programming

language (Python)

4. Test the Program◦ Write appropriate tests &

see if you get the right answers

◦ Use the Debugging system

3ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 4: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Defining the Problem

Celsius-Fahrenheit Conversion◦ Objective

◦ The temperature is given in Celsius, user wants it expressed in degrees Fahrenheit.

◦ Input◦ Temperature in Celsius

◦ Output◦ Temperature in Fahrenheit

◦ Process◦ 𝐹𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡 =

9

5𝐶𝑒𝑙𝑠𝑖𝑢𝑠 + 32

4ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 5: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Creating a Design

Algorithm◦ a well-defined recipe for solving a problem

◦ Has a finite number of steps

◦ Completes in a finite amount of time

◦ Often referred to as “pseudocode”

◦ Pseudocode is a dummy language.◦ It has no defined instruction. But it’s instructions must be completely clear

◦ It must be completely detailed.

◦ It is easily convertible to programming languages.

5ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 6: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Creating a DesignCelsius-Fahrenheit Algorithm1. Begin

2. C ← input from user

3. F ← C x 9/5 + 32

4. Output F

5. End

6ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 7: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Creating a Design

Flowchart◦ A graphical model to represent an algorithm

◦ Steps are shown with boxes of different shapes

◦ The flow is specified by arrows

7ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Terminal

Input/output

Decision

Begin

Input C

Process

F ← C x 9/5 + 32

Output F

End

Page 8: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Class Grade Average

Problem Definition◦ Objective

◦ Given the grades of N students of a class, the user wants the average grade.

◦ Input◦ Grades of students (N grades)

◦ Output◦ The average grade

◦ Process◦ 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 =

1

𝑁 𝑖=1𝑁 𝐺𝑖

8ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 9: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Class Grade AverageAlgorithm1. Begin

2. N ← input from user

3. i ← 0

4. Sum ← 0

5. G ← input from user

6. Sum ← Sum + G

7. i ← i + 1

8. If i < N then go to step 5

9. Average ← Sum / N

10. Output Average

11. End

9ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 10: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Class Grade AverageFlowchart

10ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Begin

Input N

i ← 0Sum ← 0

Input G

End

Sum ← Sum + Gi ← i +1

i < N

Average ← Sum / N

Output Average

Yes No

Page 11: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Some Problems

Problem Objectives◦ Given 3 numbers, the user wants average of them.

◦ The user wants the summation of 1 to 10.

◦ Given a number N, the user wants the average and summation of odd numbers less or equal to N.

◦ Given 3 numbers, the user wants to know if the can be triangle side lengths or not.

◦ Given N grades, the users wants the number of grades in each following periods:Grade ≥ 15 → High 15 > Grade ≥ 10 → Mid 10 > Grade → Low

11ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 12: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Some Problems

Problem Objectives◦ Given a number, the user wants reversed form of that number.

◦ Given a number, the user wants to know if it is prime or not.

◦ Given a number, the user wants the prime numbers less than that number.

◦ The user wants to know in how many ways he can have 100$ using 10$, 20$ and 50$ bills.

◦ Given 4 numbers, the user wants to know if they can form an arithmetic progression or not. If so, the user wants the next N numbers of this arithmetic progression.

12ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 13: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Coding with PythonWhen you start Python, you will see something like:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.

>>>

The “>>>” is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.

13ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 14: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Coding with PythonThe first Program: Printing a line of text◦ The print function sends a stream of text to the standard output

>>> print("Hello world")

Hello world

14ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 15: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Coding with PythonThe second program: Ask user’s name◦ The input function receives a stream of text from standard input

>>> user = input(“What’s your name? ")

What’s your name? Ali

>>> print("Hello", user)

Hello Ali

15ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 16: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Variables in Python>>> user = input(“What’s your name? ")

◦ user is an example of a variable

◦ A variable is used to assign a name to a value so that we can refer to it later.

◦ Variables have names, called identifiers

16ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 17: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Variables in Python

A variable name (identifier) can be any one word that:◦ Consists of letters, numbers, or _

◦ Does not start with a number

◦ Is not a Python reserved word (keyword)

◦ Python is case-sensitive:◦ User is not the same as user

17ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

and del from not while as elif global

or with assert else if pass yield break

except import print class exec in raise continue

finally is return def for lambda try

Page 18: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Data Types

Python provides some basic or primitive data types◦ Numeric

◦ int 25, -32, 0, 1024

◦ float 1.5, 0.0025, -80.635

◦ complex 1+3j, -2.5+j, 12j

◦ Boolean◦ True

◦ False

◦ String◦ “this is a string”, “25”, “False”

18ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 19: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Arithmetic Operations

Symbol Meaning Precedence

+ Addition Low

- Subtraction Low

* Multiplication Medium

/ Division Medium

// Floor Division Medium

% Reminder (mod) Medium

**Exponentiation

(Power)High

19ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 20: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

A Complete Program

Celsius-Fahrenheit Python Program

>>> c = input("Enter temperature in Celsius: ")

>>> f = c * 9/5 + 32

>>> print("Temperature in Fahrenheit:", f)

20ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 21: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

A Complete Program

Celsius-Fahrenheit Python Program

>>> c = input("Enter temperature in Celsius: ")

>>> f = c * 9/5 + 32

>>> print("Temperature in Fahrenheit:", f)

Any Problems?◦ The input function returns a string

◦ Needs to be converted into float

◦ >>> c = float(c)

21ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 22: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Type Conversion

Data type converter functions

22ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

◦ To Integer>>> x = int("25")

>>> x = int(34.287)

>>> x = int(True)

◦ To Float>>> x = float("34.287")

>>> x = float(12)

>>> x = float(False)

◦ To String>>> x = str(34.287)

>>> x = str(12)

>>> x = str(True)

◦ To Boolean>>> x = bool("text")

>>> x = bool(0)

>>> x = bool(34.287)

Page 23: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Put All Together

Celsius-Fahrenheit Python Program

>>> c = input("Enter temperature in Celsius: ")

>>> f = float(c) * 9/5 + 32

>>> print("Temperature in Fahrenheit:", f)

Want to save your program?◦ Let’s switch to script mode!

23ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 24: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Python Running Modes

Interactive Mode◦ Write code directly in interpreter command line shell

◦ Gives immediate feedback for each statement

◦ Best for playing around with your code

Script Mode◦ Write code into a file and save it with .py extension

◦ Give the file as input to the interpreter

◦ Best for running the program multiple times

24ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 25: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Test the Program

Programming often leads to Error!◦ Programming errors are called bugs

◦ Tracking the bugs and correcting them is called debugging

Different error types◦ Syntax error

◦ Runtime error

◦ Semantic error

25ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 26: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Programming Errors

Syntax Error◦ Caused by the violation of rules and structure of Python language

◦ Found when the code is being interpreted to machine language.

◦ Pretty easy to catch

◦ Example: choosing a keyword as a name for a variable

>>> lambda = 1.5

File "<stdin>", line 1

lambda = 1.5

^

SyntaxError: invalid syntax

26ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 27: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Programming Errors

Runtime Error (Exception)◦ Caused due to many reasons, such as hardware failure, access

violation, memory errors, …

◦ Found while the code is running

◦ Moderate to catch (need coding)

◦ Example: using operators with incorrect operand types

>>> x = "text"

>>> x / 2

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: unsupported operand type(s) for /: 'str'

and 'int'

27ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 28: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Programming Errors

Semantic Error◦ Caused due to incorrect coding or design

◦ No error messages are generated at all!

◦ Tricky to catch (need code inspection)

◦ Example: incorrect use of operator

>>> c = input("Enter temperature in Celsius: ")

>>> f = float(c) * 9//5 + 32

>>> print("Temperature in Fahrenheit:", f)

28ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 29: Fundamentals of Programming (Python) Getting …ce.sharif.edu/courses/96-97/2/ce153-4/resources/root...Some Problems Problem Objectives Given 3 numbers, the user wants average of them

Experimental Debugging

An important skill◦ Frustrating but challenging

Sounds like detective work◦ Follow the clues to find the source of error

Get help from a debugger◦ IDEs usually come with a debugger

◦ Helps to find bugs using debugging tools such as breakpoints, profilers, etc.

29ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018