Podcast
Questions and Answers
Anything you put in quotes is a string.
Anything you put in quotes is a string.
True (A)
What is the result of printing "Our logo "GO BEARS GO" "
in Python?
What is the result of printing "Our logo "GO BEARS GO" "
in Python?
Error
Which function returns the value between the parentheses as an integer?
Which function returns the value between the parentheses as an integer?
- float()
- abs()
- int() (correct)
- str()
Which function returns the value between the parentheses as a string?
Which function returns the value between the parentheses as a string?
What is the result of int(3/2)
?
What is the result of int(3/2)
?
What is the result of str(5)
?
What is the result of str(5)
?
What is the result of printing "sum is" + 15
?
What is the result of printing "sum is" + 15
?
What is the result of printing "3"*5
?
What is the result of printing "3"*5
?
What is the result of printing int("3") *5
?
What is the result of printing int("3") *5
?
When should you use double quotes (") when defining a string?
When should you use double quotes (") when defining a string?
Name three variable types in Python.
Name three variable types in Python.
Which of the following is NOT a rule for naming variables in Python?
Which of the following is NOT a rule for naming variables in Python?
What is the variable type that should be assigned to store a person's age?
What is the variable type that should be assigned to store a person's age?
What is the variable type that should be assigned to store PI?
What is the variable type that should be assigned to store PI?
What is the variable type that should be assigned to store the distance betwen Toronoto and Montreal?
What is the variable type that should be assigned to store the distance betwen Toronoto and Montreal?
In the context of math operators, what does //
do?
In the context of math operators, what does //
do?
To calculate the square root of a number, which math function would you use?
To calculate the square root of a number, which math function would you use?
Which math function returns the factorial of x?
Which math function returns the factorial of x?
Flashcards
"
"
Represents double quotes within a string.
'
'
Represents a single quote within a string.
\
\
Represents a backslash character.
\n
\n
Signup and view all the flashcards
\t
\t
Signup and view all the flashcards
\b
\b
Signup and view all the flashcards
int()
int()
Signup and view all the flashcards
float()
float()
Signup and view all the flashcards
str()
str()
Signup and view all the flashcards
Variable
Variable
Signup and view all the flashcards
Integer (int)
Integer (int)
Signup and view all the flashcards
String (str)
String (str)
Signup and view all the flashcards
Float
Float
Signup and view all the flashcards
Boolean
Boolean
Signup and view all the flashcards
Rules for Variable Names
Rules for Variable Names
Signup and view all the flashcards
Integer (int)
Integer (int)
Signup and view all the flashcards
String (str)
String (str)
Signup and view all the flashcards
Floating Point (float)
Floating Point (float)
Signup and view all the flashcards
Boolean
Boolean
Signup and view all the flashcards
math.sqrt(x)
math.sqrt(x)
Signup and view all the flashcards
math.pi
math.pi
Signup and view all the flashcards
math.ceil(x)
math.ceil(x)
Signup and view all the flashcards
math.fabs(x)
math.fabs(x)
Signup and view all the flashcards
math.factorial(x)
math.factorial(x)
Signup and view all the flashcards
math.floor(x)
math.floor(x)
Signup and view all the flashcards
math.pow(x, y)
math.pow(x, y)
Signup and view all the flashcards
math.cos(x)
math.cos(x)
Signup and view all the flashcards
math.hypot(x, y)
math.hypot(x, y)
Signup and view all the flashcards
math.sin(x)
math.sin(x)
Signup and view all the flashcards
math.tan(x)
math.tan(x)
Signup and view all the flashcards
Study Notes
- Anything within quotes is a string.
- The computer interprets strings differently from numbers.
- Using quotes indicates to the computer that something should be treated as words rather than numbers.
- Without quotes, the computer assumes the content is numeric.
- There is a difference between "4+3" (string) and 4+3 (numbers).
Escape Characters
- Escape characters serve special functions within strings.
\"
represents a double quote.\'
represents a single quote.\\
represents a backslash.\n
represents a new line.\t
represents a tab.\b
represents a backspace.print("Our logo "GO BEARS GO" ")
will result in an error because of unescaped double quotes within the string.print("Our logo \"GO BEARS GO\" ")
will output:Our logo "GO BEARS GO"
.
Type Casting
int()
converts the value within the parentheses to an integer, rounding down if necessary.float()
converts the value within the parentheses to a floating-point number, adding.0
if necessary.str()
converts the value within the parentheses to a string.int(3/2)
results in 1.float(3)
results in 3.0.str(5)
results in "5".int("7")
results in 7.int("abc")
will result in an error because "abc" is not a valid integer.print("sum is" + 15)
will result in an error because you cannot directly concatenate a string with an integer.print("sum is" + str(15))
will not have errors, because the integer has been converted to a string.print("3"*5)
results in "33333".print(int("3") * 5)
results in 15.
Using Strings
- Both single quotes (
'
) and double quotes ("
) can define strings. - Double quotes are best when the string contains a single quote.
- Single quotes are best when the string contains a double quote.
Variables
- Variables are named memory locations for storing data of a specific type.
- Known variable types so far include:
int
(integers)str
(string)float
(real numbers)
- Python declares variables "on the fly."
- Variables should be declared at the top of the program for clarity.
- Example:
age = int()
name = str()
Rules for Variables
- Variable names cannot contain spaces.
- The first character of a variable name cannot be a digit; it must be a letter (a-z or A-Z) or an underscore (_).
- Remaining characters can include letters, digits, and underscores.
- Special symbols are not allowed in variable names, except for the underscore.
- Uppercase and lowercase characters are distinct (case-sensitive).
- Reserved words in python cannot be used as variables.
- Descriptive variable names are recommended.
Variable Types
- Integer (
int
) stores integer numbers. - String (
str
) stores letters and words. - Floating point (
float
) stores decimals. - Boolean stores (
True
orFalse
) values.
What Var Type it Should Be:
- Your age:
int
- Your name:
str
- Pi:
float
- The distance between Toronto and Montreal:
float
orint
(depending on desired precision) - Today's Date:
str
or a custom date object - The number of apples in a bushel:
int
Math Operators
-
//
divides two numbers and discards the remainder (integer division). -
**
indicates exponents (e.g.,2**3
is 2 to the power of 3). -
%
gives the remainder after division (modulus). For example,7 % 3 = 1
. -
7 // 3 = 2
. -
Division by zero is not allowed.
-
math.sqrt()
will return the square root of a number. -
math.pi
represents the mathematical constant π (approximately 3.141592...). -
math.ceil(x)
returns the ceiling of x, which is the smallest integer greater than or equal to x. For example,math.ceil(3.5)
returns 4, andmath.ceil(-5.3)
returns -5. -
math.fabs(x)
returns the absolute value of x. -
math.factorial(x)
returns x factorial. Raises ValueError if x is not integral or is negative. -
Examples:
math.factorial(3)
returns 6.math.factorial(4)
returns 24. -
math.floor(x)
returns the floor of x, the largest integer less than or equal to x. For example,math.floor(3.5)
returns 3, andmath.floor(-5.3)
returns -6. -
math.pow(x, y)
returns x raised to the power y. For example,math.pow(2,3)
returns 8. -
math.cos(x)
returns the cosine of x radians. -
math.hypot(x, y)
returns the Euclidean norm, which is the length of the vector from the origin to point (x, y). It is calculated assqrt(x*x + y*y)
. -
math.sin(x)
returns the sine of x radians. -
math.tan(x)
returns the tangent of x radians. -
math.degrees(x)
converts angle x from radians to degrees. -
math.radians(x)
converts angle x from degrees to radians.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore strings and escape characters. Learn how computers interpret strings differently from numbers. Understand the use of escape characters such as ", \, \n, and type casting with int() and float().