soloLearn variables
Variable
Lesson 14.1
What's the output of this code if the user enter '42' as input: age = int(input()) print(age+8)
50
Taking User Input
Lesson 16.1
Working with Input
Lesson 17.1
Input()
You can then provide a string to input() between the parentheses, producing a prompt message.
Variable Names
You can use letters, numbers, and underscores in variable names. But you can't use special symbols, or start the name with a number. Note: Python is a case sensitive language. Which means, Lastname and lastname are two different variable names.
What's the result of this code? x = "a" x *= 3 print(x)
aaa
Fill in the blanks to create a variable "age" and assign the value 42 to it: ____________=____________
age = 42
Input
input statement work, it needs to be followed by parentheses.
Input
input() multiple times to take multiple user inputs.
Fill in the blanks to prompt the user with a message and assign the input to the variable "name": __________=_________("_________")
name = input ("What is your name")
Fill in the blanks to take a name as input and output a Welcome message, containing the input? name =____________ _________("Welcome, "+________)
name = input() print ("Welcome," + name)
x = "spam" print(x) x += "eggs" print(x)
spam spameggs
Fill in the blank to take user input, assign it to a variable, and output it: text = _________ print(_________) Choice to pick from: input() text print user x
text = input() print(text)
When you go out to eat, you always tip 20% of the bill amount. But who's got the time to calculate the right tip amount every time? Not you that's for sure! You're making a program to calculate tips and save some time. Your program needs to take the bill amount as input and output the tip as a float. Sample Input 50 Sample Output 10.0
tip = bill *.2 y = int(tip)+17 print(float(tip))
Working with Variables Variables can be reassigned as many times as you want. Note: But Beware! Overwriting the same variable with different data types is not good practice. To avoid mistakes, try to avoid doing it.
x = 123.456 print(x) x = "This is a string" print(x + "!")
Fill in the blanks to declare a variable, add 5 to it and print its value. x = 4 x _____= 5 print ______
x = 4 x + = 5 print (x)
Drag the correct answer to output 12. x = 5 y =__________ print(x + y) choice "7" =7 7
x = 5 y = 7 print(x + y)
Working with Variables So, you've got your variable, and named it. You're now free to use them to perform corresponding operations, just as you do with numbers and strings. x = 7 print(x) print(x + 3) print(x)
x = 7 print(x) print(x + 3) print(x)
Fill in the blanks to produce a valid code, which takes the values of the x and y variables from the input and outputs their sum. x = int(______()) y =_______ (input()) print(x+_____)
x = int ( input ()) y = int ( input ()) print( x + y )
age = 42 print("His age is " + str(age))
His age is 42
What is the output of this code? x = 4 x *= 3 print(x)
12
What's the output of this code? x = 3 num = 17 print(num % x)
2
x = 2 print(x) x += 3 print(x) ___________________________________________________ Example Code x = 5 print(x) x +=10 print(x) Note if you do x and y it will print x, but adding to y will give you error in code variable must be the same x to x and y to y
2 5 _________________________________ Example Code 5 15
What will be the output of this code? x = "2" y = "4" z = int(x) + int(y) print(z)
6
What's the output of this code? spam = "7" spam = spam + "0" eggs = int(spam) + 3 print(float(eggs)) Choice 10.0 73.0 703
73.0
What's the output of this code? x = 5 y = x + 3 y = int(str(y) + "2") print(y) Why its 82 is 5 + 3 = 8 when plug into str(y) + "2" the number value isn't 10 for adding but combine together because "2" is a string thus become 82.
82
Variable
A variable lets you store a value by assigning it to a name. The name can be used to refer to the value later in the program. Note To assign a variable, use one equals sign example user = "James"
Which of these is a valid variable name in Python? a-variable%name A_VARIABLE_NAME 123
A_VARIABLE_NAME
In-Place Operators
Allow you to simplify codes Note: In-place operators can be used for any numerical operation (+, -, *, /, %, **, //). So many symbols, so many possibilities!
name = input("Enter your name:") print("Hello," + name) ____________________________________________________ Example Code name = input("Enter your name: ") print("and his name is John Cena da da da da, " + name)
Enter your name: Hello, _________________________________ Example Code Enter your name: and his name is John Cena da da da da,
Lesson 18.1
In-Place Operators
In-place operators
In-place operators let you write code like 'x = x + 3' more concisely, as 'x += 3'.
x = input() print(x) ____________________________________________________ Example Code x = input (900) print(900)
Note: Even if the user enter a number as input it's processed as a string. _________________________________ Example Code 900900
Module 3
Quiz
Input
The input function prompts the user for input, and returns what they enter as a string.
What's the output of this code? spam = "eggs" print(spam * 3) eggseggseggs spamspamspam "spamspamspam" when [spam * 3] spam is equal to eggs thus eggs is to the 3rd power because spam = eggs thus it time the eggs three time together
eggseggseggs