Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Intro to: Computers & Programming Print Statements and Data Types V22.0002-001Adam MeyersNew York University Introduction to: Computers & Programming Print Statements and Data TypesIntro to: Computers & Programming Print Statements and Data Types V22.0002-001Outline• Print Statements in Python–The 'Hello World' Program–Print is a function that takes a string as an argument•Data types: strings, integers, floats, etc.• Introducing Functions, Operators and VariablesIntro to: Computers & Programming Print Statements and Data Types V22.0002-001Hello World• Custom when learning programming language• Write a simple program that causes the string 'Hello World' to be printed to the screen.• In Python 3–print('Hello World')• Syntax of print–All-lowercase “print” followed by–Parentheses containing what is to be printed–Strings to be printed are surrounded by quote marks–Multiple items to print can be separated by commasIntro to: Computers & Programming Print Statements and Data Types V22.0002-001Now let's do it wrong• Suppose we don't use quotes–Invalid syntax–One can only print legitimate Python objects• Such as strings (others will be discussed) •Suppose we use double quotes–In Python, quotes, double quotes or even triple quotes are allowed–But they have to match• Suppose we capitalize the first letter in “Print”–Lowercase “print” is the name of a function–Capitalized “Print” is not the name of a functionIntro to: Computers & Programming Print Statements and Data Types V22.0002-001What is a function in math?• A mapping from each element in one set (the domain) to exactly one element in another set (the range)• Examples–The square of a number• Given any number (the domain), there is exactly one square in the range, a subset of the set of numbers–The shoe size of a human being• For each member of the human race, there is exactly one shoe size (ignoring differences between brands, etc.)• In Python (and other languages), a function is not exactly the same thing, but sort ofIntro to: Computers & Programming Print Statements and Data Types V22.0002-001 Functions, Procedures, Subroutines, Methods, etc.• In Programming, all these terms are used to describe essentially a command, defined in terms of a set of statements of that language.• A Function can provide a mapping from input (domain) to output (range)–Like the mathematical definition–Input can be a set of zero or more items–Output can be a set of zero or more items• In programming, functions can have side effectsIntro to: Computers & Programming Print Statements and Data Types V22.0002-001In Python 3.0, “print” is a function• Input: zero or more objects as input–Zero strings causes a newline to be printed• Output: None (a special object)•Means something like 'no value'• Programmers don't normally use the output of 'print'• In some languages (C,C++), such functions are called void functions• Side effect–it prints out the input strings•In IDLE, it prints them on a line–The side effect is the important feature of this functionIntro to: Computers & Programming Print Statements and Data Types V22.0002-001Data Types•There is a function “type” that maps objects to their data type•Use this function in IDLE as follows:• type('Hello')• type(“Hello”)• type(5)• type(5.4)• type(print('hello'))–Notice the side effect of this statement•Built in data types:• Numeric types: integer, float, complex• Sequence types: string, list, tuples• Other: Boolean, NoneType (the type of None), ...Intro to: Computers & Programming Print Statements and Data Types V22.0002-001Strings in Python• A string is a sequence of characters surrounded by• Single quotes 'string'• Double quotes “string”•Triple quotes '''string'''• Embedding 1 kind of quote within another• 'the string “Python 3.1” labels this language'• “the string 'Python 3.1' labels this language”• Newlines can be embedded in triple quotes•'''These two strings, 'Python 3.1' and“Python 3.1”, are on separate lines'''•Embedded strings are called 'string literals'Intro to: Computers & Programming Print Statements and Data Types V22.0002-001Numeric Data Types• Integer– Whole numbers (positive and negative)– -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5• Float (decimal numbers)–-2.305, -0.1334, 0.33333333, 14.789, 5.0• Many-digit floats are shortened (all programming languages do this since floats can be infinitely long).• Long decimals are truncated, rounded or written in scientific notation• Try: .11111111111111111111119 and 1111111111111111111.9• Complex –Sum of floats and multiples of imaginary numbers••4.1+3j, 1+ 45j, … j=−1Intro to: Computers & Programming Print Statements and Data Types V22.0002-001Operators are Functions with the Syntax of ArithmeticOperation Standard Arithmetic Python ResultAddition 5 + 3 5+3 8Subtraction 5 - 3 5-3 2Multiplication 5 X 3 or 5 * 3 5*3 15Division 5  3 or 5 / 3 5/3 1.6666666Exponents 535**3 225Integer Division 5  3 or 5 / 3 5//3 1Modulus 5 mod 3 5%3 2Intro to: Computers & Programming Print Statements and Data Types V22.0002-001Operator Syntax vs. Function Syntax• Each operator takes 2 arguments–Syntax of Function: F(arg1, arg2)–Syntax of Operator: arg1 Op arg2•Python (and other languages) adopt this syntax because it is intuitive due to our education•Other languages like LISP adopt Polish Notation–(+ 5 3), (-5 3), (* 5 3), (expt 5 3), (mod 5 3)Intro to: Computers & Programming Print Statements and Data Types V22.0002-001Some Math from Grade School You May Have Forgotten About• Order of Operations: PEMDAS–Parentheses, Exponents,Multiplication/Division, Addition/Subtraction–What does 2+5*2 equal?• 12 or 14?•Integer Division and Modulus in Python–5  3 == 1 r 2•5//3 → 1 (integer division)• 5%3 → 2 (modulus)Intro to: Computers & Programming Print Statements and Data Types


View Full Document

NYU CSCI-UA 0002 - Print Statements and Data Types

Download Print Statements and Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Print Statements and Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Print Statements and Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?