Python numbers, converting, concatenation and math

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

What is the result of 10 + 2.5?

  • 10.2
  • 102.5
  • 12.5 (correct)
  • 12

Which of the following is an example of a float in Python?

  • 3.14 (correct)
  • 3
  • "3.14"
  • None of the above

What is the result of 9 // 2?

  • 3
  • 4.5
  • 5
  • 4 (correct)

If x = 7 and y = 2, what does x * y equal?

<p>14 (D)</p> Signup and view all the answers

How do you denote a complex number in Python?

<p>5 + 2j (B)</p> Signup and view all the answers

Which function converts a string to an integer?

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

What is the result of int("45") + float("5.5")?

<p>50.5 (A)</p> Signup and view all the answers

If num = 10.25, what does str(num) return?

<p>&quot;10.25&quot; (A)</p> Signup and view all the answers

What does float("3") return?

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

What is the result of int("123abc")?

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

What is the output of "Hello" + " " + "World"?

<p>&quot;Hello World&quot; (A)</p> Signup and view all the answers

Which operator is used to concatenate two strings in Python?

<ul> <li>(D)</li> </ul> Signup and view all the answers

What is the result of "Python" + str(3)?

<p>&quot;Python3&quot; (A)</p> Signup and view all the answers

How can you combine the following into a single string? a = "Learn" b = "Python" c = "3"

<p>All of the above (D)</p> Signup and view all the answers

Which of these would cause an error?

<p>&quot;Python&quot; + 3 (B)</p> Signup and view all the answers

What is the result of 5 * (2 + 3)?

<p>25 (A)</p> Signup and view all the answers

If a = 8 and b = 4, what does a / b equal?

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

What is the value of (4 + 6) * 2 - 8 / 2?

<p>16 (D)</p> Signup and view all the answers

How does Python handle division by zero?

<p>Raises a ZeroDivisionError (D)</p> Signup and view all the answers

What is the result of int(4.9)?

<p>4 (C)</p> Signup and view all the answers

What does 10 % 3 return?

<p>1 (A)</p> Signup and view all the answers

Which of the following is the modulus operator in Python?

<p>% (D)</p> Signup and view all the answers

If x = 25, what is x % 2?

<p>1 (D)</p> Signup and view all the answers

What is the length of "Python"?

<p>6 (A)</p> Signup and view all the answers

Given s = "coding", what does s[2] return?

<p>d (D)</p> Signup and view all the answers

Which character is at index -1 in the string "Hello"?

<p>o (C)</p> Signup and view all the answers

What is the result of "Python"[::-1]?

<p>&quot;nohtyP&quot; (A)</p> Signup and view all the answers

What is len(" ")?

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

Which method converts all characters in a string to uppercase?

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

What does "hello".capitalize() return?

<p>&quot;Hello&quot; (B)</p> Signup and view all the answers

How do you remove leading and trailing whitespace from a string?

<p>.strip() (D)</p> Signup and view all the answers

What does "Python".startswith("Py") return?

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

What does "Python".find("y") return?

<p>1 (A)</p> Signup and view all the answers

If s = "abcdef", what does s[1:4] return?

<p>&quot;bcd&quot; (D)</p> Signup and view all the answers

What is the result of "abcdef"[::-1]?

<p>&quot;fedcba&quot; (A)</p> Signup and view all the answers

What does s[:3] return if s = "hello"?

<p>&quot;hel&quot; (B)</p> Signup and view all the answers

If s = "hello", what is s[-3:-1]?

<p>&quot;ll&quot; (D)</p> Signup and view all the answers

What is s[::2] if s = "python"?

<p>&quot;pyn&quot; (C)</p> Signup and view all the answers

How can you check if "apple" is in "pineapple pie"?

<p>&quot;apple&quot; in &quot;pineapple pie&quot; (A)</p> Signup and view all the answers

What does "hello".endswith("lo") return?

<p>True (D)</p> Signup and view all the answers

How would you check if a string only contains digits?

<p>Both A and B (C)</p> Signup and view all the answers

What does "Python" in "I love Python" return?

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

Which method checks if a string contains only alphabetical characters?

<p>.isalpha() (A)</p> Signup and view all the answers

What does "hello world".find("world") return?

<p>6 (D)</p> Signup and view all the answers

How do you verify if a string is entirely uppercase?

<p>.isupper() (A)</p> Signup and view all the answers

What does "apple".islower() return?

<p>True (D)</p> Signup and view all the answers

What does "abc123".isalnum() return?

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

Which of the following checks if a string is empty?

<p>Both A and C (A)</p> Signup and view all the answers

Flashcards

What is a float in Python?

A number with a decimal point.

What does '//' do?

It performs floor division, returning the integer part of the division.

Complex Numbers in Python

It is represented using 'j' or 'J'. Example: 5 + 2j

Convert to integer

The int() function converts a string or number to an integer.

Signup and view all the flashcards

Convert to string

The str() function converts a number to a string.

Signup and view all the flashcards

Convert to float

The float() function converts a string or number to a floating-point number.

Signup and view all the flashcards

Concatenate strings

The + operator concatenates strings.

Signup and view all the flashcards

Division by zero

ZeroDivisionError is raised when dividing by zero.

Signup and view all the flashcards

What does the modulus operator (%) return?

It is the remainder of a division operation.

Signup and view all the flashcards

What does len() do?

The len() function returns the number of characters in a string.

Signup and view all the flashcards

What does string slicing do?

It returns a portion of a string

Signup and view all the flashcards

String slicing [::-1]

It reverses a string.

Signup and view all the flashcards

What does endswith() do?

The endswith() method checks if a string ends with a specified suffix.

Signup and view all the flashcards

What does isdigit() do?

The isdigit() method confirms if all characters are digits

Signup and view all the flashcards

What does isalpha() do?

The isalpha() method checks if all characters are alphabetical.

Signup and view all the flashcards

Study Notes

Python Numbers

  • 10 + 2.5 equals 12.5
  • 3.14 is an example of a float in Python
  • 9 // 2 equals 4 (integer division)
  • If x = 7 and y = 2, then x * y equals 14
  • In Python, a complex number is denoted as 5 + 2j

Python Converting

  • The int() function converts a string to an integer
  • int("45") + float("5.5") equals 50
  • If num = 10.25, str(num) returns "10.25"
  • float("3") returns 3.0
  • int("123abc") results in an Error

Python Concatenation

  • "Hello" + " " + "World" outputs "Hello World"
  • The + operator is used to concatenate two strings in Python
  • "Python" + str(3) results in "Python3"
  • To combine a = "Learn", b = "Python", and c = "3" into a single string, use f"{a}{b}{c}"
  • "Python" + 3 will cause an error

Basic Math

  • 5 * (2 + 3) equals 25
  • If a = 8 and b = 4, then a / b equals 2.0
  • (4 + 6) * 2 - 8 / 2 equals 16
  • Python raises a ZeroDivisionError if you divide by zero
  • int(4.9) equals 4

Basic Modulus

  • 10 % 3 returns 1
  • The modulus operator in Python is %
  • If x = 25, x % 2 is 1
  • 15 % 5 equals 0
  • 7 % 4 returns 3

Strings

  • The length of "Python" is 6
  • Given s = "coding", s[2] returns 'd'
  • The character at index -1 in the string "Hello" is 'o'
  • "Python"[::-1] produces "nohtyP"
  • len(" ") is 1

String Methods

  • .upper() converts all characters in a string to uppercase
  • "hello".capitalize() returns "Hello"
  • .strip() removes leading and trailing whitespace from a string
  • "Python".startswith("Py") returns True
  • "Python".find("y") returns 1

Indexing and Slicing

  • If s = "abcdef", s[1:4] returns "bcd"
  • "abcdef"[::-1] produces "fedcba"
  • If s = "hello", s[:3] returns "hel"
  • If s = "hello", s[-3:-1] returns "ll"
  • If s = "python", s[::2] produces "pto"

Checking Strings

  • To check if "apple" is in "pineapple pie", use "apple" in "pineapple pie"
  • "hello".endswith("lo") returns True
  • To check if a string contains only digits, use .isdigit()
  • "Python" in "I love Python" returns True
  • .isalpha() checks if a string contains only alphabetical characters
  • "hello world".find("world") returns 6
  • .isupper() verifies if a string is entirely uppercase
  • "apple".islower() returns True
  • "abc123".isalnum() returns True
  • To check if a string is empty, either len(string) == 0 or string == ""

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser