1 / 13

Introduction to Computer Science – Chapter 3

Introduction to Computer Science – Chapter 3. CSc 2010 Spring 2011 Marco Valero. Overview. Program Structure Names and Values Loops. Program Structure. from myro import * init() <any other imports> <function definitions> def main(): <expression> <expression> main(). Speak.

ama
Télécharger la présentation

Introduction to Computer Science – Chapter 3

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. Introduction to Computer Science – Chapter 3 CSc 2010 Spring 2011 Marco Valero

  2. Overview • Program Structure • Names and Values • Loops

  3. Program Structure from myro import * init() <any other imports> <function definitions> def main(): <expression> <expression> main()

  4. Speak • print enables us to output text to the screen • print(“hello world”) • speak enables us to output speech from the speakers • speak(“hello world”)

  5. Names • Names are identifiers for functions, parameters, and variables • Case Sensitive • Must begin with an underscore or a letter, followed by any sequence of underscores, letters, or numbers • myFunction • _myFunction • my_function1 • My_Function1

  6. Values • Variables are names for values • speed = 0.75 • funFactor = 10 • Values can be a number, or a string • <variable name> = <expression>

  7. Expressions • Expressions are a combination of values, variables, operators, and function calls that are evaluated to give a result • 5 + 3 • “hello “ + “world” • math.fabs(-3.333) * 3 • 10.0 / 3.0 • 10 / 3

  8. Number Types • Integer (whole number) • 3 • 10 • -10 • Floating point (decimal number) • 3.333 • 10.0 • 2.123e10

  9. Strings (Text Types) • Strings are the type to represent a “string” of characters • “dog” • “one fish two fish” • Literal values must be enclosed in either single quote, double quote, or triple quote • ‘this is a string literal’ • “this is a string literal” • ‘’’this is a string literal’’’

  10. Strings (Text Types) • Triple quotations allow for multiline string literals • ‘’’this is a multi Line string literal’’’ • Strings can be combined, or concatenated with + • “party” + “time” results in “partytime”

  11. Input • World Population Growth • What if we want different parameters? • <variable name> = input(<prompt string>) • funFactor = input(“what is fun factor? ”) • catCount = input(“how many cats? ”) • input() Vs raw_input()

  12. Loops • Repetition is a common need • Loops are a way of repeating some behavior • For loops for <variable> in <sequence>: <do something> for i in range(10): party()

  13. While Loops • Provide another mechanism to loop while timeRemaining(<count>): <do something> while True: <do something>

More Related