1/20
Looks like no tags are added yet.
print()
displays text or output values
automatically ends line with a new line
end=” ” parameter
allows you to print consecutive print statements on same line of code
if you want a space between the statements you must include space w/in the quotation marks
In what simple way could you update this code to display the following print statements on the same line?
print("I think I like")
print("apples")Add the end parameter to first print statement
print("I think I like", end=" ")
print("apples")What is the difference between end=" " and and=""
space b/w quotation marks adds a space between separate print statements when the end parameter is used.
for example:
# NO SPACE
print("I like going to the mall", end="")
print("to window shop.")
#Output: I like going to the mallto window shop.
# WITH SPACE
print("I like going to the mall", end=" ")
print("to window shop.")
#Output: I like going to the mall to window shop.
How do you combine text and numbers in a single print statement?
by separating them w/in print statement with commas
#Example
print("I am", 35, "years old.")Name the 3 major uses for comments.
To explain your code
To make code more readable
To prevent execution when testing code
T or F: A variable is created the moment you first assign a value to it.
True
T or F: In python, variable don’t need to be declared with any particular data type.
True
T or F: In Python, the data type of a variable can be changed after they’ve been set.
True.
What is “Type Casting”?
AKA “Casting”
Converting a variable or value from one data type to another
1) Explicit Type Casting → using Python’s built-in constructor functions (e.g. int() or bool() )
x = str(100) # Converts integer 100 to string "100"
y = str(4.5) # Converts float 4.5 to string "4.5"2) Implicit Type Casting → Python’s interpreter converts data types automatically
#Each line of code below demonstrates implicit casting
x = 5 # Integer
y = 2.5 # Float
result = x + y # Python automatically casts 'x' to 5.0
print(result) # Output: 7.5 (Float)Which type of language is Python:
(a) Dynamically Typed
(b) Statically Typed
Briefly explain what this means.
(a) Dynamically Typed →
The runtime environment (or interpreter) checks the data type of variables/values in the program at runtime (as the code runs, as opposed to before the code runs).
Explicit data types aren’t required (e.g. x =5; don’t need to state that 5 is an integer)
Data type of variable can change over time
Errors are only caught when line of code is executed
T or F: Variable names are case sensitive.
True
type()
returns data type of a variable/object
IMP NOTE: You must use the print() function with type() function in order for the data type to actually be displayed in output
minPassed = 50
print(type(minPassed)) #Output: <class 'int'>
lettersLeft = ["a", "p", "x", "z"]
print(type(lettersLeft)) #Output: <class 'list'>Rules for Python variables
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
A variable name cannot be any of the Python keywords.
Write a single line of code where:
each letter a through d is a variable
each variable holds string, which is the name of an animal starting with same letter
a, b, c, d = "ant", "bull", "cow", "dolphin"How would you assign the same value of “Orange” to each of the variables x, y, and z — in one single line of code?
x = y = z = "Orange"
print(x)
print(y)
print(z)Write the appropriate code:
Given the list fruits = ["apple", "banana", "cherry"], assign a fruit to each separate variable: (x, y, and z) using a single line of code.
Then print each variable on a separate line.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)What are the 2 ways to print multiple variables in a single print statement?
What’s the difference?
w/in print statement — separate each variable w/ a comma or a plus sign:
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)x = 5
y = "John"
print(x, y)commas
automatically adds space at end of each variable
you can also print multiple variables of different data types, but with plus symbol you can only print variables of same data type togher
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)x = 5
y = "John"
print(x + y)
#RESULTS IN ERRORWhat will happen when the following code is run?
x = 5
y = "John"
print(x + y)How can this be fixed?
Python will give you an error because + symbol can’t be used to print variables of diff data types in same print statement.
Instead use commas to separate the variables.