objectives
play

Objectives Review algorithms Programming in Python Data types - PDF document

Objectives Review algorithms Programming in Python Data types Expressions Variables Arithmetic Jan 11, 2019 Sprenkle - CSCI111 1 Review What is an algorithm? What did we learn from the PB&J demonstration? Jan


  1. Objectives • Review algorithms • Programming in Python Ø Data types Ø Expressions Ø Variables Ø Arithmetic Jan 11, 2019 Sprenkle - CSCI111 1 Review • What is an algorithm? • What did we learn from the PB&J demonstration? Jan 11, 2019 Sprenkle - CSCI111 2 1

  2. Review: Parts of an Algorithm • Input, Output • Primitive operations Ø What data you have, what you can do to the data • Naming Ø Identify things we’re using An overview for the • Sequence of operations • Conditionals semester! Ø Handle special cases • Repetition/Loops • Subroutines Ø Call, reuse similar techniques Jan 11, 2019 Sprenkle - CSCI111 3 Discussion of PB&J • The computer: a blessing and a curse Ø Recognize and meet the challenge! • Be unambiguous, descriptive Ø Must be clear for the computer to understand Ø “Do what I meant ! Not what I said!” • Motivates programming languages • Creating/Implementing an algorithm Ø Break down pieces Ø Try it out Ø Revise Jan 11, 2019 Sprenkle - CSCI111 4 2

  3. Discussion of PB&J • Steps need to be done in a particular order • Be prepared for special cases Ø Any other special cases we didn’t discuss? • Aren’t necessarily spares in real life Ø Need to write correct algorithms! • Reusing similar techniques Ø Do the same thing with a little twist • Looping Ø For repeating the same action Jan 11, 2019 Sprenkle - CSCI111 5 Other Lessons To Remember • A cowboy’s wisdom: Good judgment comes from experience Ø How can you get experience? Ø Bad judgment works every time • Program errors can have bad effects Ø Prevent the bad effects--especially before you turn in your assignment! Jan 11, 2019 Sprenkle - CSCI111 6 3

  4. Computational Problem Solving 101 • Computational Problem: A problem that can be solved by logic • To solve the problem: Ø Create a model of the problem Ø Design an algorithm for solving the problem using the model Ø Write a program that implements the algorithm Jan 11, 2019 Sprenkle - CSCI111 7 Why Do We Need Programming Languages? • Computers can’t understand English Ø Too ambiguous Live Jazz! • Humans can’t easily write machine code Problem Statement (English) Machine code/Central Processing Unit (CPU) 000000 00001 00010 00110 00000 100000 Jan 11, 2019 Sprenkle - CSCI111 8 4

  5. Why Do We Need Programming Languages? • Computers can’t understand English Ø Too ambiguous • Humans can’t easily write machine code Programmer (YOU!) Problem Statement (English) translates from problem to algorithm Algorithm/Pseudocode (solution) to program High-level Programming Language (Python) Python interpreter translates into bytecode Bytecode Machine code/Central Processing Unit (CPU) Jan 11, 2019 Sprenkle - CSCI111 9 Why Do We Need Programming Languages? • Computers can’t understand English Ø Too ambiguous • Humans can’t easily write machine code Programmer (YOU!) Problem Statement (English) translates from problem to algorithm Algorithm/Pseudocode (solution) to program Python interpreter High-level Programming Language (Python) translates into bytecode Bytecode Python interpreter executes the bytecode in a “virtual machine” Machine code/Central Processing Unit (CPU) Jan 11, 2019 Sprenkle - CSCI111 10 5

  6. Programming Languages • Programming language: Ø Specific rules for what is and isn’t allowed Ø Must be exact Ø Computer carries out commands as they are given • Syntax : the symbols given • Semantics : what it means • Example: Ø III * IV means 3 × 4 which evaluates to 12 Ø cp src dest means copy the file named src to dest • Programming languages are unambiguous Jan 11, 2019 Sprenkle - CSCI111 11 Another Syntax and Semantics Example What does this syntax mean? Jan 11, 2019 Sprenkle - CSCI111 12 6

  7. Python Is … • A programming language Ø 3 rd most popular programming language, according to Tiobe survey http://www.tiobe.com/tiobe-index/ • An interpreter (which is a program) that understands and executes Python code Jan 11, 2019 Sprenkle - CSCI111 13 Python Programming Language • A common interpreted programming language Ø Runs on many operating systems • First released by Guido van Rossum in 1991 • Named after Monty Python’s Flying Circus • Minimalist syntax, emphasizes readability • Flexible, fast, useful language • Used by scientists, engineers, systems programmers Jan 11, 2019 Sprenkle - CSCI111 14 7

  8. Python Interpreter 1. Validates Python programming language expression(s) • Enforces Python syntax • Reports syntax errors 2. Executes expression(s) • Runtime errors (e.g., divide by 0) • Semantic errors (not what you meant ) Python Expression Interpreter Only if no syntax errors Executable Output bytecode Jan 11, 2019 Sprenkle - CSCI111 15 Review: Parts of an Algorithm • Input, Output • Primitive operations Ø What data you have, what you can do to the data • Naming Ø Identify things we’re using • Sequence of operations • Conditionals Ø Handle special cases • Repetition/Loops • Subroutines Ø Call, reuse similar techniques Jan 11, 2019 Sprenkle - CSCI111 19 8

  9. Printing Output • print print is a special command or a function Ø Displays the result of expression(s) to the terminal Ø Automatically adds a '\n' (carriage return) after it’s printed • Relevant when have multiple print statements • print("Hello, class") string literal Syntax : a set of double quotes Semantics : represents text Jan 11, 2019 Sprenkle - CSCI111 20 Printing Output • print print is a special command Ø Displays the result of expression(s) to the terminal • print("Hello, class") print automatically print adds a '\n' (carriage string literal return) after it’s printed • print("Your answer is", 4*4) Syntax : comma Semantics : print multiple “things” in one line Jan 11, 2019 Sprenkle - CSCI111 21 9

  10. Parts of an Algorithm • Input, Output è Primitive operations Ø What data you have, what you can do to the data • Naming Ø Identify things we’re using • Sequence of operations • Conditionals Ø Handle special cases • Repetition/Loops • Subroutines Ø Call, reuse similar techniques Jan 11, 2019 Sprenkle - CSCI111 22 Primitive Data Types • Primitive data types represent data Ø In PB&J example, our data had types slice of bread, PB jar, jelly jar, etc. • Python provides some basic or primitive data types • Broadly, the categories of primitive types are Ø Numeric Ø Boolean Ø Strings Jan 11, 2019 Sprenkle - CSCI111 23 10

  11. Numeric Primitive Types Python Data Description Examples Type Plain integers (32-bit -214, -2, 0, 2, 100 int int precision) .001, -1.234, 1000.1, 0.00, 2.45 float float Real numbers Imaginary numbers (have complex complex 1j * 1J à (-1+0j) real and imaginary part) Jan 11, 2019 Sprenkle - CSCI111 24 How big (or small or precise) can we get? • Computer cannot represent all values • Problem: Computer has a finite capacity Ø The computer only has so much memory that it can devote to one value. Ø Eventually, reach a cutoff • Limits size of value PI has more decimals, • Limits precision of value but we’re out of space! 0 0 0 0 0 3 .1 4 1 5 9 2 6 5 Example: in Python interpreter, .1 + .1 + .1 yields 0.30000000000000004. * In reality, computers represent data in binary. Jan 11, 2019 Sprenkle - CSCI111 25 11

  12. Strings: str str • Indicated by double quotes " " or single quotes ' ' • Treat what is in the " " or ' ' literally Ø Known as string literals • Examples: Ø "Hello, world!" Ø 'c' Ø "That is Buddy's dog." Single quote must be inside double quotes* * Exception later Jan 11, 2019 Sprenkle - CSCI111 26 Booleans: bool bool • 2 values Ø True Ø False • More on these later… Jan 11, 2019 Sprenkle - CSCI111 27 12

  13. What is the value’s type? Value Type 52 -0.01 4+6j "3.7" 4047583648 True 'false' Jan 11, 2019 Sprenkle - CSCI111 28 What is the value’s type? Value Type 52 int -0.01 float 4+6j complex "3.7" str 4047583648 int True boolean 'false' str Jan 11, 2019 Sprenkle - CSCI111 29 13

  14. Parts of an Algorithm • Input, Output • Primitive operations Ø What data you have, what you can do to the data • Naming Ø Identify things we’re using • Sequence of operations • Conditionals Ø Handle special cases • Repetition/Loops • Subroutines Ø Call, reuse similar techniques Jan 11, 2019 Sprenkle - CSCI111 30 Introduction to Variables • Variables save data/information Ø Example: first slice of bread or knife A Ø Type of data the variable holds can be any of primitive data types as well as other data types we’ll learn about later • Variables have names, called identifiers Jan 11, 2019 Sprenkle - CSCI111 31 14

  15. Variable Names/Identifiers • 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 • Examples: for for while while def def • Python is case-sensitive: Ø change isn’t the same as Change Jan 11, 2019 Sprenkle - CSCI111 32 Variable Name Conventions • Variables start with lowercase letter • Convention: Constants (values that won’t change) are all capitals Ø (more on this later…) • Example: Variable for the current year Ø currentYear Naming doesn’t matter to computer, matters to humans Ø current_year Ø CURRENT_YEAR Ø currentyear Harder to read Ø current year No spaces allowed Jan 11, 2019 Sprenkle - CSCI111 33 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend