Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

1.1 Background

1.
b. Each image in the animation represents a different type of computer program.
2.
a. This type of analysis requires basic knowledge of programming.
3.
c. Like mobile apps, wi-fi speakers run small programs that connect to servers.
4.
b. This book assumes readers understand the basics of algebra, but nothing too difficult.
5.
c. Python's syntax is more concise than Java's.
6.
a. Python does not require a semicolon at the end of each line. Braces are also not required.

1.2 Input/output

1.
c. The hello world program is just one line of code.
2.
c. The end="" option removes the newline character after hello, producing Hello world! on the same line.
3.
c. The default space character that separates multiple items has been replaced with the dash character.
4.
c. The user input is stored in memory and can be referenced using today_is.
5.
b. The print() function outputs "You entered:" followed by a space character, followed by the input data, Sophia, which is referenced by the variable name.
6.
a. The input is stored exactly as the user enters the input. So, the retrieved value is displayed exactly as entered by the user.

1.3 Variables

1.
c. This would produce the output string, "The city where you live is Chicago" .
2.
b. These two lines of code would produce the output string, "Total = 6" .
3.
c. The single = character is the assignment operator.
4.
a. An assignment statement has the variable name on the left side and the value on the right side of the assignment statement.
5.
a. median is a descriptive name for a variable that stores a median value.
6.
b. A variable name can contain, but not start with, a digit.
7.
c. The name clearly indicates the value being stored is a zip code.
8.
a. Snake case separates each word with an underscore and uses only lowercase letters.

1.4 String basics

1.
c. A string that is enclosed by matching double quotes is a valid string. Note that '7 days' is also a string.
2.
c. The string, fred78@gmail.com, is enclosed in matching double quotes, and is assigned to the variable email. A string can contain characters such as "@" or "." in addition to letters and numbers.
3.
c. The string has matching double quotes. Since a single quote is a part of this string, double quotes must be used.
4.
c. Note that the enclosing single quotes (') are not part of the string but the double quotes (") are.
5.
c. The len() function returns the number of characters in the string. 6 characters exist in "Hi Ali" which are "H", "i", " ", "A", "l", and "i".
6.
b. The length of an empty string equals 0.
7.
b. The number_of_digits variable's value is 2 , and as such, the output is "Number 12 has 2 digits." .
8.
c. The 1 and 0 are enclosed in quotes indicating valid strings, and the + is not, indicating a concatenation operation. Thus, the string "10" is produced. Note that concatenation operator does not include any default separator characters like the space character.
9.
c. "A" and "wake" are valid strings, and the + is not enclosed in quotes, indicating a concatenation operation. Thus the string "Awake" is produced. Note that the concatenation operator does not include any default separator characters like the space character.
10.
c. Note that space characters must be explicitly included when using the concatenation operator.
11.
c. A concatenation of the two strings is assigned to the variable holiday. Note that "one-sided" would assign the same string to holiday.

1.5 Number basics

1.
c. x = 1 is an integer, and when printing the type of an integer variable, the output is <class 'int'>.
2.
b. y = 2.0 is floating-point number.
3.
a. Any value defined within quotation marks is a string value regardless of the content.
4.
c. Line 2 of the code evaluates as follows: c = x - z = 7 - 2 = 5. Line 3 increments variable c by 1, making the total equal to 6.
5.
b. The output of 3.5-1.5 equals 2.0 , which is a floating-point number.
6.
c. The result of the division operation is a floating-point number that isn't rounded down. 7/2 = 3.5.
7.
c. The output of the 20/2 equals 10.0 because the division operation results in a float.
8.
c. The output of an arithmetic operator on any floating-point operand results in a floating-point value. 2 * 1.5 = 3.0.
9.
a. 3 raised to the power 2 is 9, times 4 is 36, plus 1 is 37.
10.
b. Although addition has lower precedence than exponentiation, the parentheses cause 1 + 3 to be evaluated first.
11.
a. In Python, exponentiation, or raising to a power, has higher precedence than making a value negative. Python's -16 result can be confusing because, in math, "-4 squared" is 16 .
12.
c. The statement uses the assignment operator (=), the negative operator (-), and the exponentiation operator (**).

1.6 Error messages

1.
b. The filename can be found after the last slash (/). Other names, like Desktop, refer to folders.
2.
b. Line 2 is mentioned immediately after the filename.
3.
b. The type of error is always the first word on the last line of the error message.
4.
a. The middle three lines are indented. In Python, lines may not begin with spaces or tabs unexpectedly.
5.
c. The quote marks are missing at the end of the input prompt.
6.
b. The name for "input" is misspelled.

1.7 Comments

1.
b. Comments help explain the purpose of code so that other programmers understand what the code intends.
2.
a. The hash character indicates the beginning of a comment.
3.
b. Having one space after the # is the community standard.
4.
c. This comment explains the intent of the code. If the code had a mistake, the programmer reading the comment might notice the mistake more easily.
5.
a. A blank line separates the input from the output.
Then again, inserting a blank line in a three-line program is generally unnecessary.
6.
c. The # symbol "comments out" the line without removing the line permanently.
7.
a. The docstring is primarily written for others who will use the program.
8.
c. Docstrings must be a valid string, not a multi-line comment. Docstrings are stored in the program's memory, but comments are ignored.
9.
b. The amount of information in this docstring is just right. In most cases, the docstring should not be longer than the code.

1.8 Why Python?

1.
a. The language was named after Monty Python. Just for fun, the official documentation makes subtle references to Monty Python comedy sketches.
2.
b. Python's first release was in 1991, after being developed in the late 1980's.
3.
c. Making Python open source early on allowed for many programmers to provide feedback.
4.
b. For 20 years, Java and C traded places as the top language until Python took over in 2021!
5.
a. The line for Python crosses the line for JavaScript in the middle of 2018.
6.
b. TIOBE started tracking language popularity in 2001, several years after web search engines became popular.
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

© Mar 15, 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.