fundamentals of programming (python) getting started...

23
Fundamentals of Programming (Python) Getting Started with Python Sina Sajadmanesh Sharif University of Technology Fall 2017 Some slides have been adapted from “Python Programming: An Introduction to Computer Science”

Upload: lenhan

Post on 25-Mar-2018

254 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

Fundamentals of Programming(Python)

Getting Started with Python

Sina SajadmaneshSharif University of Technology

Fall 2017

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

Page 2: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

Outline1. Software Development Paradigm

2. Defining the Problem

3. Creating a Design

4. Coding with Python

5. Testing the Program

2SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 3: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

3SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 4: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

4SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 5: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

Creating a Design

Algorithm◦ a well-defined recipe for solving a problem

◦ Has a finite number of steps

◦ Completes in a finite amount of &me

◦ Often referred to as “pseudocode”

Celsius-Fahrenheit Algorithm1. Begin

2. C ← input from user

3. F ← C x 9/5 + 32

4. Output F

5. End

5SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 6: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

6SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Terminal

Input/output

Decision

Begin

Input C

Process

F ← C x 9/5 + 32

Output F

End

Page 7: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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.

7SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 8: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

8SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 9: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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? Sina

>>> print("Hello", user)

Hello Sina

9SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 10: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

10SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 11: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

11SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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 12: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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”

12SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 13: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

Arithmetic Operations

Symbol Meaning Precedence

+ Addition Low

- Subtraction Low

* Multiplication Medium

/ Division Medium

// Floor Division Medium

% Reminder (mod) Medium

**Exponentiation

(Power)High

13SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 14: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

A Complete Program

Celsius-Fahrenheit Python Program

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

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

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

14SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 15: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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)

15SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 16: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

Type Conversion

Data type converter functions

16SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

◦ 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 17: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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!

17SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 18: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

18SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 19: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

19SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 20: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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

20SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 21: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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'

21SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 22: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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)

22SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 23: Fundamentals of Programming (Python) Getting Started …ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/2... · Fundamentals of Programming (Python) Getting Started with

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.

23SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017