Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Variable and I and O - Introduction to Computer Science I | CS 110, Study notes of Computer Science

Material Type: Notes; Class: Intro to Computer Science I; Subject: Computer Science; University: University of San Francisco (CA); Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-e86-1
koofers-user-e86-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Variables and I/O
Types
Strings
En closed in quotation marks
“H ello, 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 typing
my_num = 6
my_string = “Hello”
another_num = my_num
Variables and Assignment
= often read as “gets the value”
my_num and another_num refer to the
same object
my_num = 6
my_string = “Hello”
another_num = my_num
6
“Hello”
my_string
my_num
another_num
Variables and Assignment
Numbers and strings are immutable
my_num = 6
my_string = “Hello”
another_num = my_num
my_num = 7
my_num = “CS”
“Hello”
my_string
my_num
another_num
7
6
Variable Names
A combination of letters, digits, and _
Must begin with a letter
Case sensitive
OKAY
csiscool, my_variable variable2
NOT OKAY
cs is cool, 2ndv ariable, print
Why not print?
pf3
pf4

Partial preview of the text

Download Variable and I and O - Introduction to Computer Science I | CS 110 and more Study notes Computer Science in PDF only on Docsity!

Variables and I/O

Types

  • Strings
    • Enclosed in quotation marks
    • “Hello, World!”
  • Integers
    • 4, 3, 5, 65
  • Floats
    • 4.5, 0.
  • What about “ 56 ”?

Variables and Assignment

  • A name that refers to a value
  • Python uses dynamic typing my_num = 6 my_string = “Hello” another_num = my_num

Variables and Assignment

  • = often read as “gets the value”
  • my_num and another_num refer to the

same object

my_num = 6 my_string = “Hello” another_num = my_num 6 my_string “Hello” my_num another_num

Variables and Assignment

  • Numbers and strings are immutable my_num = 6 my_string = “Hello” another_num = my_num my_num = 7 my_num = “CS” my_string “Hello” my_num another_num 7 6

Variable Names

  • A combination of letters, digits, and _
  • Must begin with a letter
  • Case sensitive
  • OKAY
    • csiscool, my_variable variable
  • NOT OKAY
    • cs is cool, 2ndvariable, print
  • Why not print?

Exercises

  1. Assign the value 9 to the variable my_num
  2. Assign the value “ 17 ” to the variable my_string
  3. Print my_num+my_string
  4. What happens?
  5. Assign the value 17 to the variable my_string
  6. Print my_num+my_string
  7. What happens?
  8. Assign the value “print” to the variable print_var
  9. What happens?

Operators

  • You’ve seen +
  • -, *, /, ** (exponentiation)
  • % - remainder
    • 12%
    • 12%
  • What is the result of 5/2?

Operators

  • What is the result of 5/2? 2
  • Why?
    • if both operands are integers, integer division is performed and the result must be an integer
    • result is truncated

Precedence

  • PEMDAS
    • parentheses
    • exponents
    • multiplication
    • division
    • addition
    • subtraction
  • Evaluation done left to right

Alternatives

  • +=, -=, *=, /=
  • num += 3 -> num = num + 3

Exercises

1. Determine the results of the following:

Exercises

1. Write the algorithm for a program that

prompts the user for two integers and

displays the sum, difference, product,

and quotient of the numbers

2. Write a program that implements the

algorithm you wrote for exercise 1

Exercises

3. Write the algorithm for a program that

stores your name, age, street number,

street name, city, state, and zip code in

separate variables and the displays the

data in the following format:

My name is : Mickey Mouse My age is: 75 My address is: 1234 Main Street, San Francisco, CA 94121

4. Write a program that implements the

algorithm you wrote for exercise 3