130 likes | 319 Views
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.
E N D
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 • print enables us to output text to the screen • print(“hello world”) • speak enables us to output speech from the speakers • speak(“hello world”)
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
Values • Variables are names for values • speed = 0.75 • funFactor = 10 • Values can be a number, or a string • <variable name> = <expression>
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
Number Types • Integer (whole number) • 3 • 10 • -10 • Floating point (decimal number) • 3.333 • 10.0 • 2.123e10
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’’’
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”
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()
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()
While Loops • Provide another mechanism to loop while timeRemaining(<count>): <do something> while True: <do something>