COMP1005/5005 Lecture 2: Strings and Lists
23 Questions
1 Views

COMP1005/5005 Lecture 2: Strings and Lists

Created by
@LuckiestIambicPentameter

Questions and Answers

What are strings?

Strings are a sequence of characters.

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

The len() function returns the length of a string.

Which characters can be considered special in a string?

  • for new line
  • for tab
  • " for quotation mark
  • All of the above (correct)
  • A string is defined using unmatched quotation marks.

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

    What is the primary operator for concatenating strings in Python?

    <ul> <li></li> </ul> Signup and view all the answers

    To create a very long string across multiple lines, we use ______ quotes.

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

    What PEP provides a style guide for Python?

    <p>PEP-8</p> Signup and view all the answers

    What does indexing allow you to do in strings?

    <p>Indexing allows you to access individual characters in a string.</p> Signup and view all the answers

    In Python, the first character of a string is accessed with index 1.

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

    What is true about lists in Python?

    <p>Lists can contain different data types.</p> Signup and view all the answers

    What operator is used to concatenate lists?

    <ul> <li></li> </ul> Signup and view all the answers

    What happens when you try to access an index in a string that does not exist?

    <p>An IndexError occurs.</p> Signup and view all the answers

    The method ______ returns a copy of the string with all uppercase letters.

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

    What does the split method return?

    <p>A list of strings</p> Signup and view all the answers

    What is the default delimiter used by the split method?

    Signup and view all the answers

    How can the split method be useful?

    <p>It is useful when working with comma separated files.</p> Signup and view all the answers

    What does the statement name[5:10] return when name = 'John Cleese'?

    <p>'Clees'</p> Signup and view all the answers

    What is the outcome of the expression name[4:] if name = 'John Cleese'?

    <p>' Cleese'</p> Signup and view all the answers

    What does the expression name[:-2] return for name = 'John Cleese'?

    <p>'John Clee'</p> Signup and view all the answers

    In slicing, what are the default values for start, stop, and step if they are omitted?

    <p>0, size, 1</p> Signup and view all the answers

    What is the primary functionality of the random module in Python?

    <p>Provides random number generation</p> Signup and view all the answers

    What does the random() function do?

    <p>Returns the next random floating point value</p> Signup and view all the answers

    What is required to use a package/module in Python?

    <p>You need to import it.</p> Signup and view all the answers

    Study Notes

    Strings

    • Strings are sequences of characters, which include alphabetical letters, numbers, symbols, and spaces.
    • Strings can be defined with single quotes (' ') or double quotes (" "). Mixing quotes leads to syntax errors.
    • Special characters like newline (\n) and tab (\t) can be used for formatting within strings. Escape characters are created with a backslash ().
    • Long strings can be split across multiple lines using the continuation character () or enclosed in triple quotes (""" """).
    • The len() function determines the length of a string, counting all characters including spaces and special characters.

    Style

    • PEP-8 outlines guidelines for writing readable Python code, emphasizing consistency.
    • Emphasizes the importance of readability, stating "Code is read much more often than it is written."
    • Comments in programs should include authorship, purpose, and modification history to enhance clarity.

    Indexing

    • Strings can be indexed to access individual characters; indexing starts at zero.
    • Negative indexing allows accessing elements from the end of the string (-1 is the last character).
    • For loops combined with range() can iterate over characters, allowing access to subranges or reversed sequences.
    • Using step in range() provides ability to skip certain characters while iterating.

    Building and Operating on Strings

    • Concatenation combines strings without automatic spacing; use '+' to join them.
    • The print function outputs strings with an optional sep parameter to determine the separator and end to specify trailing characters.
    • The in and not in operators check membership; methods like s.upper(), s.lower(), and s.replace() alter string contents.

    Lists

    • Lists store collections of data, which can include different data types, including other lists.
    • Lists maintain order, allow element access via indexing, and can be modified (adding, deleting).
    • Python supports nested lists, where lists can contain other lists as elements.
    • The .split() method transforms strings into lists based on specified delimiters; the default is a space.

    Slicing

    • Slicing permits accessing portions of strings or lists: mystring[start:stop:step].
    • Omitting any part defaults to the entire string/list, with start defaulting to 0 and stop to the length of the string/list.
    • Steps can be negative, allowing reverse slicing (e.g., name[::-1] gives a reversed string).

    Random Number Generation in Python

    • The length of a slice for non-negative indices is determined by the difference between the two indices.
    • The Python random module facilitates the generation of random numbers, including floating point numbers, integers, and selections from collections.
    • Random floating point numbers can be generated using the random() function, which produces values between 0 and 1.
    • Random integer generation can also be performed using functions within the random module.

    Pseudorandom Numbers

    • Pseudorandom number generators (PRNGs) create sequences that mimic randomness but are generated deterministically from an initial seed.
    • Seeding a PRNG establishes a starting point for the sequence, ensuring reproducibility in random number generation.
    • Good PRNGs exhibit characteristics like uniform distribution and long cycles before repeating values.

    Random Selection

    • Random selection from a series of items can be easily achieved with built-in functions in the random module.
    • Website random.org provides true random numbers based on atmospheric noise, distinguishing it from typical PRNGs which are algorithmically derived.

    Utilizing Packages and Modules

    • Python's functionality can be enhanced by importing packages and modules created by others, allowing access to a wider range of pre-written code.

    Studying That Suits You

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

    Quiz Team

    Description

    This quiz covers the essential concepts of strings and lists in the context of the COMP1005/COMP5005 Fundamentals of Programming course. It includes topics such as string building and operations, indexing, slicing, and introduction to Monte Carlo techniques. Test your understanding of these foundational programming elements!

    More Quizzes Like This

    Java Programming: Strings and Variables
    18 questions
    Python Strings and Lists
    18 questions

    Python Strings and Lists

    MeticulousBlankVerse avatar
    MeticulousBlankVerse
    C++ Programming II: Strings and Data Types
    10 questions
    Use Quizgecko on...
    Browser
    Browser