x = 2
x = x * x
y = x + 1
print('x =', x)
print('y =', y)
x = 4 y = 5
# Swap variables - NOT DO
x = 1
y = 2
y = x
x = y
print('x =', x)
print('y =', y)
x = 1 y = 1
# Swap variables - DO
x = 1
y = 2
temp = y
y = x
x = temp
print('x =', x)
print('y =', y)
x = 2 y = 1
hi = "hello there"
greetings = 'hello'
foo = "this isn't right"
name = 'eric'
greet = hi + name
greeting = hi + ", " + name
print(greet)
print(greeting)
hello thereeric hello there, eric
'ab' + 'cd'
'abcd'
3 * 'eric'
'ericericeric'
len('eric')
4
len('hi there')
8
'eric'[1]
'r'
'eric'[0]
'e'
name = 'eric'
name[0]
'e'
name[4]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-12-766a7f505528> in <module>() ----> 1 name[4] IndexError: string index out of range
'eric'[1:3]
'ri'
'eric'[:3]
'eri'
'eric'[1:]
'ric'
'eric'[:]
'eric'
x = 1
print(x)
x_str = str(x)
print("my fav num is", x, ".", "x = ", x)
print("my fav num is" + x_str + "." + "x = " + x_str)
print("my fav num is " + x_str + ". " + "x = " + x_str)
1 my fav num is 1 . x = 1 my fav num is1.x = 1 my fav num is 1. x = 1
text = input("Type something...")
print(5 * text)
type(text)
Type something...hello hellohellohellohellohello
str
num = int(input("Type a number..."))
print(5 * num)
Type a number...10 50
n = input('You are in the Lost Forest. Go left or right?')
while n == 'right':
n = input('You are in the Lost Forest. Go left or right?')
print('You got out of the Lost Forest!')
You are in the Lost Forest. Go left or right?right You are in the Lost Forest. Go left or right?right You are in the Lost Forest. Go left or right?left You got out of the Lost Forest!
####################
## EXAMPLE: while loops
## Try expanding this code to show a sad face if you go right
## twice and flip the table any more times than that.
## Hint: use a counter
####################
#n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
#while n == "right" or n == "Right":
# n = input("You are in the Lost Forest\n****************\n****** ***\n (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
#print("\nYou got out of the Lost Forest!\n\o/")
n = 0
while n < 5:
print(n)
n = n + 1
0 1 2 3 4
for n in range(5):
print(n)
0 1 2 3 4
mysum = 0
for i in range(7, 10):
mysum += i
print(mysum)
24
mysum = 0
for i in range(5, 11, 2):
mysum += i
print(mysum)
21
mysum = 0
for i in range(5, 11, 2):
mysum += i
if mysum == 5:
break
print(mysum)
5
total = 0
for c in '123456789':
total = total + int(c)
print(total)
45
x = 3
ans = 0
itersLeft = x
while (itersLeft != 0):
ans = ans + x
itersLeft = itersLeft - 1
print(str(x) + '*' + str(x) + ' = ' + str(ans))
3*3 = 9
Replace the comment in the following code with a while loop.
numXs = int(input('How many times should I print the letter X? '))
toPrint = ''
#concatenate X to toPrint numXs times
print(toPrint)
How many times should I print the letter X? 4
Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.
# Find a cube root of x
x = int(input('Enter an integer: '))
ans = 0
while ans ** 3 < x:
ans = ans + 1
if ans ** 3 != x:
print(str(x) + ' is not a perfect cube')
else:
print('Cube root of ' + str(x) + ' is ' + str(ans))
Enter an integer: 27 Cube root of 27 is 3
# Find a cube root of x
x = int(input('Enter an integer: '))
ans = 0
while ans ** 3 < abs(x):
ans = ans + 1
if ans ** 3 != abs(x):
print(str(x) + ' is not a perfect cube')
else:
if x < 0:
ans = -ans
print('Cube root of ' + str(x) + ' is ' + str(ans))
Enter an integer: 29 29 is not a perfect cube
cube = 27
for guess in range(cube + 1):
if guess**3 == cube:
print('Cube root of', cube, 'is', guess)
Cube root of 27 is 3
cube = 8
for guess in range(abs(cube) + 1):
if guess**3 >= abs(cube):
break
if guess**3 != abs(cube):
print(cube, 'is not a perfect cube')
else:
if cube < 0:
guess = -guess
print('Cube root of ' + str(cube) + ' is ' + str(guess))
Cube root of 8 is 2
Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect.
Let s be a string that contains a sequence of decimal numbers separated by commas, e.g., s = '1.23,2.4,3.123'. Write a program that prints the sum of the numbers in s.