Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

This chapter introduced the basics of programming in Python, including:

  • print() and input().
  • Variables and assignment.
  • Strings, integers, and floats.
  • Arithmetic, concatenation.
  • Common error messages.
  • Comments and docstrings.

At this point, you should be able to write programs that ask for input, perform simple calculations, and output the results. The programming practice below ties together most topics presented in the chapter.

Function Description
print(values)
Outputs one or more values, each separated by a space, to the user.
input(prompt)

If present, prompt is output to the user. The function then reads a line of input from the user.

len(string)
Returns the length (the number of characters) of a string.
type(value)

Returns the type (or class) of a value. Ex: type(123) is <class 'int'>.

Operator Description

=
(Assignment)

Assigns (or updates) the value of a variable. In Python, variables begin to exist when assigned for the first time.

+
(Concatenation)

Appends the contents of two strings, resulting in a new string.

+
(Addition)

Adds the values of two numbers.

-
(Subtraction)

Subtracts the value of one number from another.

*
(Multiplication)

Multiplies the values of two numbers.

/
(Division)

Divides the value of one number by another.

**
(Exponentiation)

Raises a number to a power. Ex: 3**2 is three squared.

Syntax Description

#
(Comment)

All text is ignored from the # symbol to the end of the line.

' or "
(String)

Strings may be written using either kind of quote. Ex: 'A' and "A" represent the same string. By convention, this book uses double quotes (") for most strings.

"""
(Docstring)

Used for documentation, often in multi-line strings, to summarize a program's purpose or usage.

Table 1.6 Chapter 1 reference.

Try It

Fun facts

Write a program that assigns a variable named number to any integer of your choice. Ex: number = 74. Then, use this variable to calculate and output the following results:

    74 squared is 5476
    74 cubed is 405224
    One tenth of 74 is 7.4
    74 plus 123 is 197
    74 minus 456 is -382
    

Run the program multiple times, using a different integer each time. Your output should be mathematically correct for any integer that you choose.

The point of this exercise is to perform basic arithmetic within a print statement. Do not use any other variables besides number. Your program should have only one assignment statement (at the beginning).

Try It

Mad lib

A mad lib is a word game in which one person asks others for words to substitute into a pre-written story. The story is then read aloud with the goal of making people laugh.

This exercise is based the Vacations Mad Lib available on the Printables section of MadLibs.com. Write a program that asks the user to input two adjectives and two nouns (user input in bold):

tranquil
    Adjective: scandalous
    Noun: pancake
    Noun: field
    
    Adjective: tranquil
    Adjective: scandalous
    Noun: pancake
    Noun: field
    

Use input() to display each prompt exactly as shown. The user's input should be on the same line as the prompt. Each colon must be followed by exactly one space. After reading the input, the program should output the following three lines:

tranquil place with your scandalous family.
    Usually you go to some place that is near a/an pancake or up on a/an field.
    
    A vacation is when you take a trip to some tranquil place with your scandalous family.
    Usually you go to some place that is near a/an pancake or up on a/an field.
    

Notice that the first line should be completely blank. Replace the bold words (from the above example) with the actual words input by the user.

Your final program should have four input statements, three print statements, and at least two comments. For completeness, write an appropriate docstring at the top of the program.

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.