Python Numbers, Data Types, 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
Download our mobile app to listen on the go
Get App

Questions and Answers

When should you use the float data type in Python?

  • For representing true or false values.
  • For storing whole numbers only.
  • For storing numbers with decimal points or fractional values. (correct)
  • For storing text or character strings.

What output does print(type(100)) produce in Python?

  • <class 'float'>
  • <class 'str'>
  • <class 'int'> (correct)
  • <class 'bool'>

Which of the following data types is most suitable for representing logical conditions in Python?

  • Integer
  • String
  • Boolean (correct)
  • Float

What is the purpose of the int() function in Python?

<p>To convert a value to an integer. (A)</p> Signup and view all the answers

Which function is used to transform a string representation of a number into an integer?

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

If you execute print(float("2.71")), what will be the output?

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

Which of the following methods correctly converts the integer 123 into a string?

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

What happens if you try to convert a non-numeric string such as int("xyz") in Python?

<p>It results in a TypeError. (A)</p> Signup and view all the answers

What is the boolean value of bool(-5) in Python?

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

What is the result of the expression 'Code' + 'cademy' in Python?

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

How would you concatenate the string "Pine" with " apple" so that there is a space in between?

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

Which expression correctly combines the string "Total: " with the number 50 to produce the string "Total: 50"?

<p>&quot;Total: &quot; + str(50) (C)</p> Signup and view all the answers

What is the outcome of concatenating the strings "25" and "75"?

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

What happens when you try to join a string directly with a number (e.g., 'Result: ' + 25 in Python)?

<p>Python signals a TypeError. (C)</p> Signup and view all the answers

Evaluate the expression: $2 + 5 * 3$

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

Which operator is used to calculate the power of a number in Python?

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

What do you get from the operation 21 // 4?

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

What is the result of the expression $(8 + 4) * 3$?

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

If you calculate $10 - 4 + 3$, what will be the output?

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

What is the remainder when 26 is divided by 7?

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

How can you verify if a number is an odd number?

<p>number % 2 == 1 (C)</p> Signup and view all the answers

What will 30 % 4 return as a result?

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

What is the output of 100 % 100?

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

How do you determine if a number can be evenly divided by 5?

<p>number % 5 == 0 (D)</p> Signup and view all the answers

How does Python classify 'Computer Science'?

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

What is needed to define a String in Python?

<p>Text enclosed in quotation marks &quot; &quot; or ' ' (C)</p> Signup and view all the answers

If you have the string Programming, what would len("Programming") return?

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

Which of the following ways can be used to properly create strings in Python?

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

Can you change individual characters within a string in Python?

<p>No, strings are immutable. (D)</p> Signup and view all the answers

What is the primary function of the .upper() method in Python when applied to a string?

<p>Transform the string to uppercase (B)</p> Signup and view all the answers

If language = 'Python', what will language[0:4] return?

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

With the string education = 'Science', which methods can locate the position for the first occurrence of the char i?

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

What character will be the output of print('Hello CS!')[5]?

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

Which code snippet reverses the word String?

<p><code>String</code>[::-1] (D)</p> Signup and view all the answers

What output does the following line of code produce? print('Function'[2:6])

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

What is the correct way to separate the string Keep, Moving, Forward into a list by the commas?

<p><code>Keep, Moving, Forward</code>.split(',') (C)</p> Signup and view all the answers

What is the new structure of soccer after applying soccer = 'Goal'.replace('o', 'a')?

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

Given word = 'Enchantment', what does word[5] display?

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

What method would accurately display how many times the substring ame is repeated in Name Game?

<p>.count('ame') (C)</p> Signup and view all the answers

Flashcards

Integer

Data type for storing whole numbers (no decimals).

type() function

Displays the data type of a variable or value.

Float

A data type that can store numbers with decimal points.

Boolean

Represents True or False values, used in logical operations.

Signup and view all the flashcards

int()

Converts a decimal number or string to an integer.

Signup and view all the flashcards

int()

Converts a string to an integer.

Signup and view all the flashcards

float()

Converts a number or a string to a floating-point number.

Signup and view all the flashcards

str()

Converts an integer to a string.

Signup and view all the flashcards

int("abc")

Raises a ValueError because "abc" is not a valid integer string.

Signup and view all the flashcards

bool(0)

Returns False because 0 is considered a 'falsy' value.

Signup and view all the flashcards

Concatenation

Joins two strings together

Signup and view all the flashcards

"AP " + "CSP"

String concatenation combines 'AP' and ' CSP' with a space in between.

Signup and view all the flashcards

"Grade:" + str(12)

Integer must be converted to string for concatenation.

Signup and view all the flashcards

"10" + "20"

It concatenates strings '10' and '20' resulting in '1020'.

Signup and view all the flashcards

"Test" + 5

Python throws a TypeError because it cannot implicitly convert an integer to a string.

Signup and view all the flashcards

3 + 4 * 2

Multiplication is performed before addition due to order of operations.

Signup and view all the flashcards

** operator

Raises a number to a power.

Signup and view all the flashcards

// operator

Integer division that returns the floor (integer) of the division result.

Signup and view all the flashcards

(5 + 3) * 2

Parentheses are evaluated first.

Signup and view all the flashcards

7 - 3 + 2

Operators are applied from left to right.

Signup and view all the flashcards

% operator

Returns the remainder of a division.

Signup and view all the flashcards

number % 2 == 0

A number is even if the remainder when divided by 2 is 0.

Signup and view all the flashcards

20 % 3

Evaluates to 2, which is the remainder when 20 is divided by 3.

Signup and view all the flashcards

10 % 10

Evaluates to 0 because 10 is perfectly divisible by 10 with no remainder.

Signup and view all the flashcards

number % 4 == 0

The expression tests if there is no remainder when 'number' divided by 4.

Signup and view all the flashcards

String

A sequence of characters.

Signup and view all the flashcards

Creating a String

Enclose text in either single quotes (' ') or double quotes (" ").

Signup and view all the flashcards

Length of a String

The number of characters in the string.

Signup and view all the flashcards

String syntax

In Python, both single quotes and double quotes can be used.

Signup and view all the flashcards

Immutable

Once created, string's value cannot be altered.

Signup and view all the flashcards

.upper()

Converts all characters in a string to uppercase.

Signup and view all the flashcards

"Python"[:3]

Extracts a portion of a string from the beginning up to (but not including) index 3.

Signup and view all the flashcards

.index() or .find()

Returns index of first instance of the character.

Signup and view all the flashcards

"Hello, World!"[-1]

Accesses the last character in the string.

Signup and view all the flashcards

"[::-1]"

Creates a reversed copy of the string.

Signup and view all the flashcards

"Computer"[1:4]

Extracts characters from index 1 up to (but not including) index 4.

Signup and view all the flashcards

.split()

Splits a string into a list of substrings based on spaces.

Signup and view all the flashcards

.replace()

Replaces all occurrences of 'a' with 'o'.

Signup and view all the flashcards

word?

Accesses character at index 3 of a string.

Signup and view all the flashcards

.count()

Counts the number of times a substring appears.

Signup and view all the flashcards

Study Notes

Numbers Data Type

  • Integer is the suitable data type for storing whole numbers.
  • type(42) outputs <class 'int'>.
  • Float is the appropriate data type for storing numbers with decimal points.
  • The boolean data type represents true or false values.
  • int() function converts a decimal number to an integer in Python.
  • int() function converts a string to an integer.
  • float("3.14") results in 3.14.
  • str(25) converts the integer 25 into a string.
  • int("abc") results in an error.
  • bool(0) returns false.

Concatenation

  • "Hello" + "World" evaluates to "HelloWorld".
  • "AP " + "CSP" correctly concatenates the string "AP" with a space and then "CSP".
  • "Grade:" + str(12) correctly combines "Grade:" and the integer 12 into the string "Grade:12".
  • "10" + "20" concatenates to result in "1020".
  • Attempting to concatenate a string and an integer directly throws a TypeError.

Math

  • 3 + 4 * 2 evaluates to 11 due to order of operations.
  • ** is the Python operator that performs exponentiation.
  • 15 // 2 results in 7 because // is the floor division operator.
  • (5 + 3) * 2 evaluates to 16 due to order of operations.
  • 7 - 3 + 2 outputs 6.

Modulus

  • 14 % 5 results in 4.
  • number % 2 == 0 determines if a number is even.
  • 20 % 3 evaluates to 2.
  • 10 % 10 evaluates to 0.
  • number % 4 == 0 checks if a number is divisible by 4.

Strings

  • "Hello, World!" is a string data type.
  • Strings are created by enclosing text in quotes " " or ' '.
  • The length of the string "Python" is 6.
  • 'AP CSP' and "AP CSP" are correct string syntax.
  • Strings in Python are immutable, meaning they are unchangeable.

String Methods

  • .upper() converts a string to uppercase.
  • "Python"[:3] returns the first three characters of "Python," resulting in "Pyt".
  • .index("e") and .find("e") both find the index of the first occurrence of "e" in "Science."
  • "Hello, World!"[-1] evaluates to "!".
  • "APCSP"[::-1] reverses the string "APCSP," resulting in "PSCPA".
  • "Computer"[1:4] results in "omp".
  • "AP CSP Exam".split() splits "AP CSP Exam" into a list by spaces.
  • "banana".replace("a", "o") produces "bonana".
  • If word = "Python", then word[2] is "t".
  • .count("i") counts occurrences of "i" in "Mississippi".

Studying That Suits You

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

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser