to Python Code CS 1111 September 4, 2019 Logistics You do not need - - PowerPoint PPT Presentation

to python code
SMART_READER_LITE
LIVE PREVIEW

to Python Code CS 1111 September 4, 2019 Logistics You do not need - - PowerPoint PPT Presentation

Going From Psuedocode to Python Code CS 1111 September 4, 2019 Logistics You do not need to do Lab02 Counting, nor can you get credit for it The labs are intended primarily for 1110 We will do roughly every other lab Our first


slide-1
SLIDE 1

Going From Psuedocode to Python Code

CS 1111 – September 4, 2019

slide-2
SLIDE 2

Logistics

  • You do not need to do Lab02 Counting, nor can you get credit for it
  • The labs are intended primarily for 1110
  • We will do roughly every other lab
  • Our first Lab will be Lab03, which we will do in-class next Wednesday
  • You must be in class to get credit
  • You are only required to attend on lab days
  • Labs are never HOMEWORK, therefore, you don’t have to do them ahead
  • First programming assignments are
  • PA1 greeting – Due Wednesday September 18 at 9a.m.
  • PA2 nonsense – Due Wednesday September 18 at 9a.m.
  • You have two weeks for this, but should be able to do PA1 after today if you wish
slide-3
SLIDE 3

Last Time: Psuedocode

  • Psuedocode is on of the methods that can be used to represent /

describe an algorithm

  • The syntax is not rigid
  • But the problem solving IS detailed, executable, and unambiguous
  • Psuedocode can be easily translated into a high-level programming

language

  • Include a specific sequence of actions that a program will take
  • Each line does a single, clear, unambiguous, executable thing
slide-4
SLIDE 4

Control Structures

  • What control structures did we discuss last time?
slide-5
SLIDE 5

Control Structures

  • Sequence
  • A series of actions performed one after another
  • Condition (if)
  • To decide which of two or more different statements to execute depending
  • n a certain condition
  • Repetition (loop)
  • Repeat a certain step while a certain condition is true
  • Subprogram / named action
  • A small part of another program used to solve a certain problem
slide-6
SLIDE 6

Example

  • Write pseudocode to solve the following problem:
  • A company is trying to have a storewide 20% off sale
  • State sales tax is 5%
  • A cashier should enter the list price which does NOT include the 20% off or

sales tax.

  • The system applies the discount, then the sales tax to the list price to

calculate the final total.

  • The system then displays the final total
slide-7
SLIDE 7

Design

Psuedocode Flow Chart

  • 1. Get item price
  • 2. Subtract 20% discount
  • 3. Add 5% sales tax
  • 4. Display final sale price

Get item price Subtract 20% discount Add 5% sales tax Display final sale price Start End

slide-8
SLIDE 8

Is this good enough?

  • Robustness testing
  • Consider inputs that are semantically meaningless, but syntactically valid
  • Syntactically valid means the types of the inputs are correct, so the pseudocode

is executable with that input

  • Semantically meaningless means that the input wouldn’t make sense from an

external perspective (buying -3 gallons of gas, or saying your birthday is 13/45/3000)

  • Price is a number
  • Numbers can be 0
  • Numbers can be negative
  • This one specifically wouldn’t make sense
  • How can we adjust our algorithm to consider robustness?
slide-9
SLIDE 9

Design

Psuedocode Flow Chart

  • 1. Get item price
  • 2. If item price < 0

1. Repeat step 1

  • 3. Subtract 20% discount
  • 4. Add 5% sales tax
  • 5. Display final sale price

Get item price Subtract 20% discount Add 5% sales tax Display final sale price Start End

false

item price < 0

true

slide-10
SLIDE 10

Python

  • Interpreted Programming Language (does not compile)
  • Has simple syntax – easy to read
  • Has most of the features of traditional programming languages
  • Supports a wide range of applications
  • Games, web, apps, system administration
  • Widely used
  • Google, IBM, Disney, EA
  • Open Source
slide-11
SLIDE 11

Psuedocode to Python

Psuedocode

  • 1. Set price = 0
  • 2. If item price <= 0

1. Get price from the user

  • 3. Subtract 20% discount
  • 4. Add 5% sales tax
  • 5. Display final sale price
slide-12
SLIDE 12

How to Program

  • Programmer uses an editor (such as PyCharm) to edit the source

code (.py file)

  • When the programmer hits run, the interpreter turns the program

into byte code (.pyc file)

  • The Bytecode is translated into machine instructions

z = 0 x = 3 y = x while x != 0: z = z + y x = x – 1 y = z ADD R3 R2 R3 SUB R0 R0 R1 BZERO 4 BRANCH 0 MOVE R2 R3 HALT

High-Level Language Assembly Language Machine Language

Compiler/ Interprette r Assembler

10100001001011 01010001111011 01110000110101 00100110101010 01110010101101 10111101011111 11111111111

slide-13
SLIDE 13

PyCharm

  • You should have installed this last week (if you haven’t, do Lab0
  • n your own ASAP!!!)
slide-14
SLIDE 14

Print()

  • print() is a Subprogram that prints to the console (bottom part
  • f the PyCharm screen)
  • You can print:
  • Text
  • Numbers
  • Any variable
  • Test
  • print("Hello World")
  • print()
  • print(5)
slide-15
SLIDE 15

Comments

  • To help ensure our code is not just readable, but understandable,

we use comments

  • Comments are ignored by the interpreter, and are there to explain our code
  • We use the ‘#’ symbol to start a comment.
  • The interpreter ignores all text after the #
slide-16
SLIDE 16

Block comments

  • We may have a large section of comments (note that because I

have no #, the entire program won’t compile!)

slide-17
SLIDE 17

Block comments

  • I could just simply add # before every line
slide-18
SLIDE 18

Block comments

  • Or, I could use block comment

1. Select all lines of code to be commented 2. If you have windows

1. Hit CTRL + /

3. If you have Mac

1. Hit Command + /

slide-19
SLIDE 19

Indentation

  • Python requires correct indentation
  • Below does not compile because of incorrect indentation
  • Notice the red squiggly line
  • You must indent for the loop, everything indented is INSIDE the

loop

  • 4 spaces
slide-20
SLIDE 20

Continuing statements over multiple lines

  • Two ways to continue a line of code
  • "/n" is used to “force” a new line (think of it like hitting enter)

# 1. Implicit continuation: divide statements after parentheses, brackets, and braces # and before or after a plus operator print("My name is Will" + " and my favorite courses are " + "\nCS 1111" + "\nCS 2110" + " and \nCS 2501 SDE") # 2. Explicit continuation: use the \ character to divide statements anywhere on a line t = "My name is Will" + " and my favorite courses are " \ + "\nCS 1111" \ + "\nCS 4640" \ + " and \nCS 2501 SDE" print(t)

slide-21
SLIDE 21

Printing quotation marks

  • “Wait, if we use quotation marks to surround text, how do we

print quotation marks?”

print("My favorite courses are : " + "'CS 1111'") # use different quatations print("My favorite courses are : " + "\'CS 1111\'") # use an escape character print("My favorite courses are : " + "\"CS 1111\"") # use an escape character

slide-22
SLIDE 22

Variables

  • A variable is a stored value that can change as a program executes
  • “Able to vary”
  • Initialize a variable with an assignment statement
  • Assignment states use the = sign
  • Set the variable on the left side to be equal to value of the right side

x = 5 # 5 is a literal y = 3 # 3 is a literal

  • The variables value can be changed later

x = 7 # x can be changed to another literal x = y # variable can also be changed to other variables x = x + 1 # variable can also be set to the value of an expression

slide-23
SLIDE 23

Type

  • There are different types of information we could want to store
  • Therefore, there are different types of variables
  • int
  • 1, 2, 3, … , 0, -1, -2
  • string
  • “hi”, “Will”, “There once was a man from Nantucket”
  • float
  • 3.14, 0.07, -0.3, 5.0
  • bool
  • True, False
slide-24
SLIDE 24

Python Variables

  • Python variables can change their data type whenever needed
  • The below code is completely valid
  • quantity1 is at first an int, then a float, then finally a string
  • After this code block, quantity1 is “15”
  • There’s no way to find out what quantity1 used to be.

quantity1 = 10 # set quantity1 to an int value of 10 quantity1 = 5.0 # set quantity1 to an int value of 5.0 quantity1 = "15" # set list_price to a str value of "15", not an int of 15

slide-25
SLIDE 25

Python Variable Naming

  • Python variable names are case sensitive
  • Variables names can start with letter or underscore
  • Not numbers or symbols
  • Variables names can contain letters or underscores or numbers
  • Not any other special characters

r = 5 # Create variable r y = R # Error because R is not a variable, but r is y = r # Valid

slide-26
SLIDE 26

Good Variable Naming Conventions

  • Start with a lowercase letter
  • Use underscore notation for variable name with multiple words
  • variable_name_with_multiple_words
  • Don’t use camelCase: variableNameWithMultipleWords
  • This is a Java-esque style: every language has it’s own style
  • Use meaningful, easy to remember names
  • At least two characters long
  • Avoid built-in words (we’ll cover these as we go through)