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 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
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
Control Structures • What control structures did we discuss last time?
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 on 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
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
Design Psuedocode Flow Chart Start 1. Get item price Get item price 2. Subtract 20% discount Subtract 20% discount 3. Add 5% sales tax 4. Display final sale price Add 5% sales tax Display final sale price End
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?
Design Start Psuedocode Flow Chart Get item price 1. Get item price true item price < 0 2. If item price < 0 false Subtract 20% discount 1. Repeat step 1 3. Subtract 20% discount Add 5% sales tax 4. Add 5% sales tax 5. Display final sale price Display final sale price End
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
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
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 High-Level Assembly Machine Language Language Language 10100001001011 z = 0 ADD R3 R2 R3 01010001111011 x = 3 SUB R0 R0 R1 y = x BZERO 4 01110000110101 while x != 0: BRANCH 0 00100110101010 Compiler/ Assembler Interprette z = z + y MOVE R2 R3 01110010101101 r 10111101011111 x = x – 1 HALT 11111111111 y = z
PyCharm • You should have installed this last week (if you haven’t, do Lab0 on your own ASAP!!!)
Print() • print() is a Subprogram that prints to the console (bottom part of the PyCharm screen) • You can print: • Text • Numbers • Any variable • Test • print("Hello World") • print() • print(5)
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 #
Block comments • We may have a large section of comments (note that because I have no # , the entire program won’t compile!)
Block comments • I could just simply add # before every line
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 + /
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
Continuing statements over multiple lines • Two ways to continue a line of code # 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) • "/n" is used to “force” a new line (think of it like hitting enter)
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
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
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
Python Variables • Python variables can change their data type whenever needed • The below code is completely valid 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 • 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.
Python Variable Naming • Python variable names are case sensitive r = 5 # Create variable r y = R # Error because R is not a variable, but r is y = r # Valid • 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
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)
Recommend
More recommend