Strings and Escape Characters
18 Questions
2 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Anything you put in quotes is a string.

True (A)

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?

  • float()
  • abs()
  • int() (correct)
  • str()

Which function returns the value between the parentheses as a string?

<p>str() (B)</p> Signup and view all the answers

What is the result of int(3/2)?

<p>1</p> Signup and view all the answers

What is the result of str(5)?

<p>&quot;5&quot;</p> Signup and view all the answers

What is the result of printing "sum is" + 15?

<p>error</p> Signup and view all the answers

What is the result of printing "3"*5?

<p>&quot;33333&quot;</p> Signup and view all the answers

What is the result of printing int("3") *5?

<p>15</p> Signup and view all the answers

When should you use double quotes (") when defining a string?

<p>When the string contains a single quote. (C)</p> Signup and view all the answers

Name three variable types in Python.

<p>int, str, float, boolean</p> Signup and view all the answers

Which of the following is NOT a rule for naming variables in Python?

<p>Variable names are case-insensitive (uppercase and lowercase characters are the same). (B)</p> Signup and view all the answers

What is the variable type that should be assigned to store a person's age?

<p>int</p> Signup and view all the answers

What is the variable type that should be assigned to store PI?

<p>float</p> Signup and view all the answers

What is the variable type that should be assigned to store the distance betwen Toronoto and Montreal?

<p>float</p> Signup and view all the answers

In the context of math operators, what does // do?

<p>Divides two numbers and discards the remainder (the decimals)</p> Signup and view all the answers

To calculate the square root of a number, which math function would you use?

<p>math.sqrt(x) (A)</p> Signup and view all the answers

Which math function returns the factorial of x?

<p>math.factorial(x) (B)</p> Signup and view all the answers

Flashcards

"

Represents double quotes within a string.

'

Represents a single quote within a string.

\

Represents a backslash character.

\n

Represents a new line.

Signup and view all the flashcards

\t

Represents a tab.

Signup and view all the flashcards

\b

Represents a backspace.

Signup and view all the flashcards

int()

Converts a value to an integer.

Signup and view all the flashcards

float()

Converts a value to a floating-point number.

Signup and view all the flashcards

str()

Converts a value to a string.

Signup and view all the flashcards

Variable

Named storage location in memory.

Signup and view all the flashcards

Integer (int)

Whole numbers (e.g., 1, 2, 3).

Signup and view all the flashcards

String (str)

Sequences of characters (e.g., "hello").

Signup and view all the flashcards

Float

Real numbers with decimal points (e.g., 3.14).

Signup and view all the flashcards

Boolean

True or False values.

Signup and view all the flashcards

Rules for Variable Names

Cannot contain spaces, special symbols (except _), or reserved keywords; must start with a letter or underscore.

Signup and view all the flashcards

Integer (int)

Stores integer numbers.

Signup and view all the flashcards

String (str)

Stores letters and words.

Signup and view all the flashcards

Floating Point (float)

Stores numbers with decimal points.

Signup and view all the flashcards

Boolean

True or False.

Signup and view all the flashcards

math.sqrt(x)

Returns the square root of a number.

Signup and view all the flashcards

math.pi

The mathematical constant π (pi).

Signup and view all the flashcards

math.ceil(x)

Returns the smallest integer greater than or equal to x.

Signup and view all the flashcards

math.fabs(x)

Returns the absolute value of x.

Signup and view all the flashcards

math.factorial(x)

Returns x factorial.

Signup and view all the flashcards

math.floor(x)

Returns the largest integer less than or equal to x.

Signup and view all the flashcards

math.pow(x, y)

Returns x raised to the power y.

Signup and view all the flashcards

math.cos(x)

Returns the cosine of x radians.

Signup and view all the flashcards

math.hypot(x, y)

Returns the Euclidean norm, sqrt(xx + yy).

Signup and view all the flashcards

math.sin(x)

Returns the sine of x radians.

Signup and view all the flashcards

math.tan(x)

Returns the tangent of x radians.

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 or False) values.

What Var Type it Should Be:

  • Your age: int
  • Your name: str
  • Pi: float
  • The distance between Toronto and Montreal: float or int (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, and math.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, and math.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 as sqrt(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.

Quiz Team

Related Documents

Python Programming Notes PDF

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().

More Like This

Use Quizgecko on...
Browser
Browser