Unformatted text preview:

COMP 116 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 Python treats double quote and single quote the same But be consistent 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 integer age 29 this is string 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 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 Illegal message What s up Doc n 17 pi 3 14159 76trombones big parade more 1000000 class Computer Science 101 MANIPULATING VARIABLES 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 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 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 EXPRESSIONS A combination of values variables and operators Operators are special symbols that represent computations Addition Subtraction Division Multiplication Exponentiation Raises a number to a power x y Remainder Modulus 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 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 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 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 For strings variable int input Prompt variable float input Prompt 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


View Full Document

UNC-Chapel Hill COMP 116 - VARIABLES, EXPRESSIONS, AND STATEMENTS

Download VARIABLES, EXPRESSIONS, AND STATEMENTS
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 VARIABLES, EXPRESSIONS, AND STATEMENTS 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 VARIABLES, EXPRESSIONS, AND STATEMENTS 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?