Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
INLS 560 – VARIABLES, EXPRESSIONS, AND STATEMENTS Instructor: Jason Carter WHAT IS A PROGRAM? Set of instructions that a computer follows to perform a task or solve a problem INSTRUCTIONS 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 HELLO WORLD EXAMPLE print “Hello, World!” Keyword Output: Hello World Value ANOTHER EXAMPLE print 1 + 1 Keyword Output: 2 Value VALUES HAVE TYPES print “Hello, World!” Keyword 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. ANOTHER TYPE EXAMPLE print 1 + 1 Keyword The value 2 is a type of integer A non-decimal numerical value There are many different types VARIABLES 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 (=) ASSIGNMENT STATEMENT 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 = 3.14159 VARIABLE NAMES AND KEYWORDS Rules for naming variables in Python: Legal 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 message = "What’s up, Doc?“ n = 17 pi = 3.14159 Illegal 76trombones = ’big parade’ more$ = 1000000 class = ’Computer Science 101’ MANIPULATING VARIABLES Variables can be: Printed message print message Output: • = "What’s up, Doc?” What’s up Doc Reassigned message = "What’s up, Doc?” message = “Hello?” print message Output: Hello? print and assignment are two kinds of statements STATEMENT 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. REVIEW Python has 29 keywords Variable Assignment statement A variable is a name that refers to a value used to create a variable and make it reference data Statement an instruction that the Python interpreter can execute EXPRESSIONS A combination of values, variables, and operators Operators are special symbols that represent computations Addition Subtraction Division Multiplication Exponentiation Performs division and returns the remainder 4%2=0, 5%2=1 Typically used to convert times and distances, and to detect odd or even numbers Evaluate the expression: 4 + 4 Raises a number to a power x ** y = 𝑥 𝑦 Remainder (Modulus) + – / * ** Output: ? Evaluate the expression: (4 + 4) - 3 % ORDER OF OPERATIONS Follows same rule of precedence as mathematics PEMDAS Parentheses Exponentiation Multiplication and Division Addition and Subtraction Evaluate the expression: (4 + 4) - 3 INPUT FROM KEYBOARD Most programs need to read input from the user Built-in input function reads input from keyboard Format: variable = input(prompt) Returns the data as a string 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: ”) READING NUMBERS WITH THE INPUT FUNCTION 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 INPUT FROM KEYBOARD For integers: For floats: variable = int(input("Prompt")) variable = float(input("Prompt")) For strings: variable = input("Prompt") PRACTICE Write a program that takes Two numbers from a user Print out the sum, difference, product, and quotient of the two numbers