1 / 20

Computer Science 101

Computer Science 101. Introduction to Programming. From Algorithms to Programs. Algorithms are abstract things that can have Linguistic realizations (in pseudocode or a programming language) Hardware realizations (in digital circuitry). What Is a Program?.

belita
Télécharger la présentation

Computer Science 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 101 Introduction to Programming

  2. From Algorithms to Programs Algorithms are abstract things that can have • Linguistic realizations (in pseudocode or a programming language) • Hardware realizations (in digital circuitry)

  3. What Is a Program? • One or more algorithms written in a programming language that can be run on hardware • Sometimes called software

  4. What Is a Programming Language? • A bit like pseudocode, but very strict syntax rules • Examples: FORTRAN, COBOL, Lisp, Basic, C, C++, Java, Python, …

  5. From Algorithms to Hardware Algorithm Translate by human being Program Translate by compiler Circuitry

  6. The Program Development Process Editor Program in programming language Compiler Syntax errors Program in machine language Circuitry Input Output

  7. Python • Free, general purpose programming language • Can run code interactively in a shell • Can also edit and run longer code segments called scripts or programs

  8. Pattern of Simple Programs • Get inputs from the user • Run an algorithm on these inputs • Print the results as outputs

  9. Basic Elements: Variables • Variable names - a sequence of letters and/or digits, can include _ (the underscore) • Case sensitive - radius and Radius name two distinct variables • Variables are usually spelled with lowercase letters, constants with uppercase letters

  10. Assignment Statement • Gives a variable a value • Variables must have values before they are used in expressions PI = 3.14 area = PI * 6 ** 2 print area <variable> = <expression> = means set

  11. Basic Elements: Keywords • Keywords are special names reserved for special purposes • print, if, else, while, import, … • Color coded in orange in IDLE

  12. Functions • Run a computation on a given value to produce a new value • The given values are called arguments >>> abs(-45) 45 >>> round(3.1416, 3) 3.142 <function>(<arg1>, …, <argn>)

  13. Numeric Data Types • int - whole numbers, such as 45, 100, -20 • float - numbers with a decimal point, such as 3.14, 66.222, -0.33

  14. Arithmetic Operators • +, -, *, /, %, ** • Evaluate from left to right, but do ** first, the *, /, %, then +, - • Use parentheses to override the standard order of evaluation • / and % produce integer quotient and remainder with two ints, or a float when at least one operand is a float

  15. Numerical Input • The user is prompted with a text message • The sequence of keystrokes is converted to an int or a float length = input("Enter the length: ") width = input("Enter the width: ") <variable> = input(<a string>) input is a function

  16. String Data Type • Use " or ' as delimiters for simple strings • Use """ as delimiters for multiline strings or comments """ File: rectangle.py Author: Ken """ length = input("Enter the length: ")

  17. Output Statements • print outputs numbers or strings • Outputs a newline by default print "My name is Ken" print "I have 15 students" print <expression>

  18. Output Statements • Use a comma to print several data values on the same line, separated by a space name = "Ken" number = 15 print "My name is", name print "I have", number, "students"

  19. Input of Text (Strings) • The user is prompted with a text message • The sequence of keystrokes is converted to a single string name = raw_input("Enter your name: ") color = raw_input("Enter your hair color: ") <variable> = raw_input(<a string>) raw_input is a function

  20. For Monday Homework due!

More Related