CS105 Study Guide

Unit 1: Introduction to Python 3

1a. Use an integrated development environment (IDE) to write simple programs

  • What is Python?
  • How can an IDE be used when practicing Python?
  • What are some conveniences when using a web-based IDE?

Every programming language must have some means, or environment, for constructing and executing programs. Python development environments are designed for practice and ease of testing so that a user can get immediate feedback and experience immediate results. After practice and testing, Python programs can be packaged and deployed for stand-alone applications using more sophisticated tools beyond what has been applied at this introductory level.

A host of integrated development environments (IDE) exist for practicing Python. Some require installation on a local computer. At the time of this writing, a few popular locally-installable environments include Microsoft Visual Studio and Jupyter Notebook. Other IDEs, such as Repl.it, are internet-based, require no installation, and can be invoked using a web browser. The Repl.it IDE has a command line window allowing for entry, practice, and testing of single Python instructions. In addition, the run window allows you to run and test Python scripts.

To review, see Introducing Python and The Repl.it IDE.


1b. Explain the int and float data types

  • What is the int data type and what is int data useful for?
  • What is the float data type and what is float data useful for?
  • What are the differences between the int and float data types?

Computers work by operating on data that is stored in memory. At the memory level, data is generally represented in the form of binary (that is, base 2) numbers. The method used to translate data from the IDE to binary memory data depends upon the data type. Differences between types of data arise precisely because they are represented differently in memory. While the details of data type representation is a subject reserved for more advanced courses, at the beginner programming level, it is still necessary to be aware of how to apply different data types.

There are several different Python data types, including int, float, and str. Since computers are often used to operate on numbers, it is sensible to begin by considering two fundamental Python numerical data types: int and float.

  • The term int stands for integer
  • The term float stands for floating point

It is recommended that, independent of any programming language, you fully understand the mathematical differences between integers and floating point numbers. After this, the programming aspects of using float data versus int data should fall into place.

To review, see Compare and Contrast int vs float and Scientific Notation for Floating Point Numbers.


1c. Explain variable assignment

  • What is variable assignment?
  • How does the Python assignment operation a=b differ from the mathematical expression a=b (that is, "a equals b")?
  • What are the important points to consider when choosing a variable name?

At the introductory level, one of the most important operations to understand is variable assignment. The assignment operator = is a powerful tool that allows you to modify the value of a variable. When used incorrectly, it can result in the loss of data due to unintentionally overwriting a value contained in a variable.

There are key elements to mastering variable assignment. First and foremost, data contained on the right side of an equals sign is assigned to the variable referred to on the left side of the equals sign. Always pay close attention to this "right-to-left" assignment convention. Second, choosing a variable name that reflects its intended use can increase the readability of your code. Third, be careful not to choose variable names that conflict with Python keywords. By employing each of these practices, you will have mastered variable assignment.

To review, see Variables and Assignment Statements, Reserved Words and Variable Naming Conventions, and Values, Types, Variable Names, and Keywords.


1d. Apply basic Python output using the print instruction

  • Why is the print instruction useful at this phase in the course?
  • What is the syntax for using the print instruction?
  • How can the print instruction be used for outputting multiple instances of data and text?

Assuming a given variable has been initialized, it is possible to output variable values using the Python command line by simply typing the name of the variable. However, when using the Python run window to run Python programs, this is not the case. To reveal the value of a variable, the print instruction can be used.

The print statement can be used to help find coding errors by providing the ability to check values. It can also be used to output the values of several variables at a time along with text data to output helpful messages to the screen. To demonstrate a full understanding of the print instruction, you must be able to accurately predict the screen output when a specific print instruction is invoked. An accurate prediction includes not just the values of variables, but also their arrangement and formatting.

To review, see Using Variables in Python.


1e. Explain the string data type

  • What is the str data type and what is str data useful for?
  • How does str data differ from int and float?
  • How can numerical data be converted into string data and vice versa?

The str data type is used to house strings of character data placed between to quotes. For example, '123&*()Python' is a character string of type str. Differences between numerical and string data must be respected or unwanted programming errors can occur. If you want to use string data that represents numerical data as a number, you must first convert it to a numerical form. For instance, the instruction float('3.9') converts the string '3.9' to a numerical floating point value. The instruction int('25') converts the string '25' to an integer value. Conversely, str(5.43) converts the floating point value 5.43 to the string '5.43'. Data type gymnastics of this kind are necessary because data is not always available in the desired form. Therefore, you must sometimes take additional steps to perform the appropriate conversions.

The Python type command is indispensable for determining the data type of a variable. Assuming you have mastered variable data types, you should be able to accurately predict the output of an instruction such as print(type(a)) for some variable a. Learning how to program involves both analysis and design. Before mastering the program design phase, you must first master the analysis phase. One important facet of program analysis is to be able to predict the end result after a snippet of code runs. Therefore, "accurately predicting" does not mean guessing at what you think may be the output. Instead, it means putting yourself in the reference frame of a computer and mentally executing each instruction as they are read in their proper sequence.

To review, see The Basics of Strings.


Unit 1 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • integrated development environments (IDE)
  • command line
  • run window
  • data types
  • numerical data types
  • string data
  • int
  • float
  • str
  • variable assignment 
  • assignment operator
  • print