Python Strings and Numbers

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

Which function would you use to determine the number of characters in a string?

  • str()
  • size()
  • count()
  • len() (correct)

The upper() function modifies a string in place, changing all characters to uppercase.

False (B)

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

remove whitespace

To split a string into a list of substrings, you would use the ______() function.

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

Match the following functions with their descriptions:

<p>int() = Converts a value to an integer float() = Converts a value to a floating-point number round() = Rounds a number to a specified number of decimal places abs() = Returns the absolute value of a number</p> Signup and view all the answers

Which of the following functions returns both the quotient and the remainder of a division operation?

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

The max() function can only be used with numerical lists.

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

How do you add an element to the end of a list in Python?

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

To insert an item at a specific position in a list, you would use the ______() method.

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

Which list method removes and returns an item from a specified index?

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

The sort() method creates a new sorted list without modifying the original list.

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

What is the primary purpose of the .keys() method when used with a dictionary?

<p>get all keys/return all keys</p> Signup and view all the answers

To add multiple key-value pairs to a dictionary at once, or to update existing values, you can use the ______() method.

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

Which operator is used to check if a key exists in a dictionary?

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

The input() function always returns an integer value.

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

What function is used to determine the data type of a variable in Python?

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

The ______() function is used to create a sequence of numbers, often used in loops.

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

Which function combines multiple lists into a single iterable of tuples?

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

The all() function returns True if any of the elements in an iterable are True.

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

What keywords are used to handle conditional execution in Python?

<p>if, elif, else</p> Signup and view all the answers

Flashcards

len()

Returns the length of a string.

str()

Converts other data types into a string.

lower()

Converts a string to lowercase.

upper()

Converts a string to uppercase.

Signup and view all the flashcards

capitalize()

Capitalizes the first letter of a string.

Signup and view all the flashcards

title()

Capitalizes the first letter of each word in a string.

Signup and view all the flashcards

split()

Splits a string into a list of substrings.

Signup and view all the flashcards

join()

Joins a list of strings into a single string.

Signup and view all the flashcards

replace()

Replaces specified characters or words in a string.

Signup and view all the flashcards

find()

Finds the index of a substring within a string.

Signup and view all the flashcards

strip()

Removes whitespace from the beginning and end of a string.

Signup and view all the flashcards

count()

Counts occurrences of a substring or character in a string.

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

round()

Rounds a number to a specified number of decimal places.

Signup and view all the flashcards

abs()

Returns the absolute value of a number.

Signup and view all the flashcards

pow(x, y)

Returns x raised to the power of y.

Signup and view all the flashcards

max()

Returns the largest item in an iterable or the largest of two or more arguments.

Signup and view all the flashcards

min()

Returns the smallest item in an iterable or the smallest of two or more arguments.

Signup and view all the flashcards

sum()

Calculates the sum of all items in an iterable.

Signup and view all the flashcards

Study Notes

Strings

  • len() gets the length of a string
  • str() converts other data types to strings
  • lower() converts a string to lowercase
  • upper() converts a string to uppercase
  • capitalize() capitalizes the first letter of a string
  • title() capitalizes the first letter of each word in a string
  • split() splits a string into a list
  • join() joins a list into a string
  • replace() replaces characters or words in a string
  • find() finds the index of a substring within a string
  • strip() removes whitespace from a string
  • count() counts occurrences of a substring or character in a string

Numbers

  • int() converts a value to an integer
  • float() converts a value to a float
  • round() rounds a number
  • abs() returns the absolute value of a number
  • pow(x, y) raises x to the power of y
  • max() finds the largest number, while min() finds the smallest number
  • sum() sums the values in a list
  • divmod(a, b) returns the quotient and remainder of a division

Lists

  • len() Returns the number of items in a list
  • list() converts other iterables to a list
  • append() adds an item to the end of a list
  • insert() inserts an item at a specific index in a list
  • pop() removes and returns an item by its index
  • remove() removes an item by its value
  • sort() sorts items in place, while sorted() returns a new sorted list
  • reverse() reverses the order of items in a list
  • count() counts the occurrences of a value in a list
  • index() gets the index of a value in a list

Dictionaries

  • dict() creates a dictionary
  • .get() retrieves a value with a fallback if the key is not found
  • .keys() retrieves all keys in the dictionary
  • .values() retrieves all values in the dictionary
  • .items() retrieves all (key, value) pairs in the dictionary
  • .update() adds or updates entries in the dictionary
  • .pop() removes an item by key
  • in checks if a key exists in the dictionary

Logic & Utilities

  • input() gets user input
  • print() prints output to the console
  • type() checks the variable type
  • isinstance() checks if a variable is of a certain type
  • range() creates a sequence of numbers
  • enumerate() gets both the index and value when looping
  • zip() combines multiple lists
  • all() returns True if all items in an iterable are True
  • any() returns True if any item in an iterable is True

Loops and Conditions

  • range() is used in for-loops to generate a sequence of numbers
  • break and continue are loop control keywords
  • if, elif, and else are used for conditionals

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