DOC PREVIEW
USF CS 110 - Variables and I/O

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Variables and I/OTypes• Strings– Enclosed in quotation marks– “Hello, World!”• Integers– 4, 3, 5, 65• Floats – 4.5, 0.7• What about “56”?Variables and Assignment• A name that refers to a value• Python uses dynamic typingmy_num = 6my_string = “Hello”another_num = my_numVariables and Assignment• = often read as “gets the value”• my_num and another_num refer to thesame objectmy_num = 6my_string = “Hello”another_num = my_num6“Hello”my_stringmy_numanother_numVariables and Assignment• Numbers and strings are immutablemy_num = 6my_string = “Hello”another_num = my_nummy_num = 7my_num = “CS”“Hello”my_stringmy_numanother_num76Variable Names• A combination of letters, digits, and _• Must begin with a letter• Case sensitive• OKAY– csiscool, my_variable variable2• NOT OKAY– cs is cool, 2ndvariable, print• Why not print?2Exercises1. Assign the value 9 to the variable my_num2. Assign the value “17” to the variable my_string3. Print my_num+my_string4. What happens?5. Assign the value 17 to the variable my_string6. Print my_num+my_string7. What happens?8. Assign the value “print” to the variableprint_var9. What happens?Operators• You’ve seen +• -, *, /, ** (exponentiation)• % - remainder– 12%6– 12%5• What is the result of 5/2?Operators• What is the result of 5/2? 2• Why?– if both operands are integers, integer divisionis performed and the result must be an integer– result is truncatedPrecedence• PEMDAS– parentheses– exponents– multiplication– division– addition– subtraction• Evaluation done left to rightAlternatives• +=, -=, *=, /=• num += 3 -> num = num + 3Exercises1. Determine the results of the following:1. 5+9/4*3-22. (5+9)/(4*(3-2))3. 5**2+1/4-44. 5**(2+1)/(4-5)5. 5**(2+1)/(4-4)6. ((4-2)/(3-8)7. ((5+3)/3(2+1))3Strings• Concatenation– print “Hello, “ + “World!”– print “Hello “ + “Class!”– print “Hello” + “Class!”• Repetition– print “Hello” * 3– print “Hello,” * 3Strings• Can be in single or double quotes– “hello” or ‘hello’• Escape sequences encode special characters– \n = newline, \t = tab, \\ = \, \” = “, \’ = ‘– can also use “ in string enclosed by ‘’ and ‘ in stringenclosed by “”• “it’s fun”, ‘a “sample” string’• ‘it\’s fun’, “a \”sample\” string”• http://docs.python.org/ref/strings.html– lists python escape sequencesExercises1. Try the following commands:1. print “\tName: Bob”2. print “\t Name:\n Bob”3. print “Name:\a Bob”4. print “\a”*10Composition• What is the result of the following:age = 19print “Your age is “ + age• Instead, use ‘,’ to compose statementsage = 19print “Your age is “, ageKeyboard Input• input(<prompt>) reads an integer/float fromthe keyboard• raw_input(<prompt>) reads a string fromthe keyboard• Syntax– variable_name = input(<prompt>)– variable_name = raw_input(<prompt>)• Examples– mynum = input(“Enter number: “)– mystring = raw_input(“Enter string: “)Keyboard Input• Examplesmynum = input(“Enter number: “)same asprint “Enter number: “mynum = input()• Recall, an int can be a string, but a stringcannot be an int4Exercises1. Write the algorithm for a program thatprompts the user for two integers anddisplays the sum, difference, product,and quotient of the numbers2. Write a program that implements thealgorithm you wrote for exercise 1Exercises3. Write the algorithm for a program thatstores your name, age, street number,street name, city, state, and zip code inseparate variables and the displays thedata in the following format:My name is : Mickey MouseMy age is: 75My address is: 1234 Main Street, San Francisco, CA941214. Write a program that implements thealgorithm you wrote for exercise


View Full Document

USF CS 110 - Variables and I/O

Download Variables and I/O
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 and I/O 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 and I/O 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?