INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter
W HAT IS A P ROGRAM ? Set of instructions that a computer follows to perform a task or solve a problem
I NSTRUCTIONS 29 keywords and. def, exec, if, not, return assert, del, finally, import, or, try break, elif, for, in, pass, while class, else, from, is, print, yield continue, except, global, lambda, raise
H ELLO W ORLD E XAMPLE print “Hello, World!” Keyword Value Output: Hello World
A NOTHER E XAMPLE print Keyword Value Output: 2
V ALUES H AVE T YPES print “Hello, World!” The value “Hello World” is a type of string A string is a sequence of characters A string can be identified because it is enclosed in quotes. Keyword
A NOTHER T YPE E XAMPLE print The value 2 is a type of integer A non-decimal numerical value There are many different types Keyword
V ARIABLES A variable is a name that refers to a value A name that represents a value stored in the computer memory Assignment statement: used to create a variable and make it reference data General format is variable = expression Example: age = 29 Assignment operator: the equal sign (=)
A SSIGNMENT S TATEMENT In an assignment statement, variable receiving value must be on left side You can only use a variable if a value is assigned to it message = "What’s up, Doc?“ n = 17 pi =
V ARIABLE N AMES AND K EYWORDS Rules for naming variables in Python: Variable name cannot be a Python key word Variable name cannot contain spaces First character must be a letter or an underscore After first character may use letters, digits, or underscores Variable names are case sensitive Variable name should reflect its use Legal message = "What’s up, Doc?“ n = 17 pi = Illegal 76trombones = ’big parade’ more$ = class = ’Computer Science 101’
M ANIPULATING V ARIABLES Variables can be: Printed message = "What’s up, Doc?” print message Output: What’s up Doc Reassigned message = "What’s up, Doc?” message = “Hello?” print message Output: Hello? print and assignment are two kinds of statements
S TATEMENT A statement is an instruction that the Python interpreter can execute print 1 x = 2 print x Output: 1 2 The assignment statement does not produce output.
R EVIEW Python has 29 keywords Variable A variable is a name that refers to a value Assignment statement used to create a variable and make it reference data Statement an instruction that the Python interpreter can execute
E XPRESSIONS
O RDER OF O PERATIONS Follows same rule of precedence as mathematics PEMDAS Parentheses Exponentiation Multiplication and Division Addition and Subtraction Evaluate the expression: (4 + 4) - 3
I NPUT F ROM K EYBOARD Most programs need to read input from the user Built-in input function reads input from keyboard Returns the data as a string Format: variable = input(prompt) prompt is typically a string instructing user to enter a value Does not automatically display a space after the prompt Example: name = input(“Please enter your name: ”)
R EADING N UMBERS WITH THE INPUT F UNCTION input function always returns a string Built-in functions convert between data types int(item) converts item to an int float(item) converts item to a float Type conversion only works if item is valid numeric value, otherwise, throws exception
I NPUT F ROM K EYBOARD For integers: variable = int(input("Prompt")) For floats: variable = float(input("Prompt")) For strings: variable = input("Prompt")
P RACTICE Write a program that takes Two numbers from a user Print out the sum, difference, product, and quotient of the two numbers