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?

  • 12.5 (correct)
  • 12
  • 10.2
  • 102.5

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?

  • 4 (correct)
  • 4.5
  • 3
  • 5

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 (C)</p> Signup and view all the answers

Which function converts a string to an integer?

<p>int() (D)</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 (A)</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>(B)</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 (C)</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 (D)</p> Signup and view all the answers

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

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

How does Python handle division by zero?

<p>Raises a ZeroDivisionError (A)</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 (B)</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 (A)</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 (B)</p> Signup and view all the answers

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

<p>o (A)</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() (D)</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 (A)</p> Signup and view all the answers

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

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

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

<p>&quot;bcd&quot; (A)</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;el&quot; (B)</p> Signup and view all the answers

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

<p>&quot;pyto&quot; (A)</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 (A)</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 (C)</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 (C)</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 (B)</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 (C)</p> Signup and view all the answers

Flashcards

What is a float?

A number with a fractional part (e.g., 3.14).

What does '//' do?

It performs floor division, discarding any remainder.

What is the int() function?

It converts a string or number to an integer.

What is the str() function?

It converts a number to a string.

Signup and view all the flashcards

What is the float() function?

It converts a string or number to a floating-point number.

Signup and view all the flashcards

How to concatenate strings?

The '+' operator.

Signup and view all the flashcards

Why use parentheses?

Parentheses change the order of operations.

Signup and view all the flashcards

What if you divide by zero?

It will raise a ZeroDivisionError.

Signup and view all the flashcards

What does the modulus operator do?

It returns the remainder of a division.

Signup and view all the flashcards

What does len() do?

It returns the number of characters in a string.

Signup and view all the flashcards

What does slicing do?

It returns a section of the string.

Signup and view all the flashcards

How to uppercase a string?

The .upper() method.

Signup and view all the flashcards

How to remove whitespace?

The .strip() method.

Signup and view all the flashcards

How to check if substring exists?

The in operator.

Signup and view all the flashcards

isalpha()

isalpha() checks if a string contains only letters.

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 in Python.
  • If x = 7 and y = 2, then x * y equals 14.
  • A complex number in Python is denoted as 5 + 2j.

Python Converting

  • The int() function converts a string to an integer.
  • int("45") + float("5.5") results in 50.5.
  • 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 "".join([a, b, c]) or f"{a}{b}{c}".
  • "Python" + 3 would cause an error.

Basic Math

  • 5 * (2 + 3) equals 25.
  • If a = 8 and b = 4, a / b equals 2.0.
  • (4 + 6) * 2 - 8 / 2 equals 16.
  • Python raises a ZeroDivisionError when dividing by zero.
  • int(4.9) results in 4.

Basic Modulus

  • 10 % 3 returns 1.
  • The modulus operator in Python is %.
  • If x = 25, x % 2 equals 1.
  • 15 % 5 results in 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] results in "nohtyP".
  • len(" ") is 1.

String Methods

  • The .upper() method 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] results in "fedcba".
  • If s = "hello", s[:3] returns "hel".
  • If s = "hello", s[-3:-1] is "ll".
  • If s = "python", s[::2] is "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() or .isnumeric().
  • "Python" in "I love Python" returns True.
  • The .isalpha() method checks if a string contains only alphabetical characters.
  • "hello world".find("world") returns 6.
  • To verify if a string is entirely uppercase, use .isupper().
  • "apple".islower() returns True.
  • "abc123".isalnum() returns True.
  • To check if a string is empty, use 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

Python Data Types
6 questions

Python Data Types

LovelyRomanArt3291 avatar
LovelyRomanArt3291
Python Data Types
7 questions

Python Data Types

AccessibleGiant avatar
AccessibleGiant
Python Data Types
5 questions

Python Data Types

ClearerCosecant avatar
ClearerCosecant
Use Quizgecko on...
Browser
Browser