Computer Science 15-110, Spring 2011
Class Notes: Console I/O
- Console Output
- Basic Print Statement
- Print with Comma
- Print Multiple Items
- Console Input
- String
with raw_input()
- Number
with raw_input() (error!)
- Number
with raw_input() and int()
- Strings and Numbers with input()
Console I/O
- Console Output
- Basic Print Statement
print "Carpe"
print "diem"
- Print with Comma
print "Carpe",
print "diem"
- Print Multiple Items
print "Carpe", "diem"
Another Example:
# Compute the hypotenuse of a right triangle
a = 3
b = 4
c = ((a**2) + (b**2))**0.5
print "side a =", a
print "side b =", b
print "hypotenuse c =", c
- Basic
Console Input
- String
with raw_input()
name = raw_input("Enter your name: ")
print "Your name is: " + name
- Number
with raw_input() (error!)
x = raw_input("Enter a number: ")
print "One half of ", x, " = ", x/2 # Error!
- Number
with raw_input() and int()
x = int(raw_input("Enter
a number: "))
print "One half of ", x, " = ", x/2
- Strings and
Numbers with input() (Not recommended!)
x = input("Try this with a string and then
a number: ") #
input() is not recommended!
print 2*x
carpe
diem - carpe
diem - carpe
diem - carpe
diem - carpe
diem - carpe
diem - carpe
diem - carpe
diem - carpe diem