Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

3.1 Strings revisited

1.
a. Since index values start at 0, the index 1 refers to the second character of the string.
2.
b. The character at index 1 is y, and the character at index -1 is !.
3.
c. The result of s[0] is "P" , which is a string of length 1.
4.
b. Code points 65 to 90 are used for uppercase letters.
5.
b. The code point for the character "0" is 48.
6.
b. Tilde (~) is the character for code point 126. The chr() function returns a string of length one.
7.
c. An escape sequence is two characters long and begins with a backslash (\).
8.
c. The first backslash indicates an escape sequence, and the second backslash indicates the character.
9.
c. The escape sequence \n produces a new line. print("Enter\nhere") produces the same result.

3.2 Formatted strings

1.
b. The same variable, says, is used in three replacement fields.
2.
a. The string is not an f-string, so the brace characters (around temp and food) are printed literally.
3.
c. Using an f-string allows the entire output to be specified without commas and extra quote marks.
4.
a. The format specifier 02d appends a 0 to the front of the integer when the integer is only one digit.
5.
c. The format specifier ".2f" (with a period) means fixed-point, rounded to two decimal places.
6.
b. This format specifier is useful for displaying large amounts of money.

3.3 Variables revisited

1.
b. Each line of the program in the animation is an assignment statement.
2.
c. The program assigns the int object 10 and the float object 9.7 , as shown in the memory diagram.
3.
a. An arrow shows which object a variable currently refers to.
4.
a. An object's identity is an integer representing the memory location.
5.
c. The type function returns an object's class name like int, float, or str.
6.
b. A variable refers to an object, so the variable's value is the object's value.

3.4 List basics

1.
b. A list of numbers can be set up using an assignment operator.
2.
a. Individual elements in a list must be separated by a comma. Python does not understand what is meant by 2 3.
3.
c. Since indexes start at 0, the required number is one less than the position being accessed.
4.
a. The index -1 always refers to the last element of a list. The expression len(name_list)-1, which evaluates to 3 and refers to the last element in a list of 4 elements, can also be used in place of -1 here.

3.5 Tuple basics

1.
a. The first element, 2 , is at index 0.
2.
c. Tuples can have elements of the same type as well as elements of different types.
3.
a. A tuple must be created using commas. Parentheses are optional but recommended. The three elements are "a" , "b" , and "c" .
4.
b. my_tuple is initially assigned with the tuple (0.693, 0.414, 3.142). Then my_tuple is assigned with the new tuple, (0.693, 1.414, 3.142), to correct a mistake with the second element.
5.
a. Tuples are immutable. No changes, including appending, can be made after the tuple is created.
6.
b. Tuples are immutable, so attempted changes produce errors. Using a tuple would protect the constants from accidental changes.
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.