Jason M. Grant

Assistant Professor of Computer Science

Introduction to Python

In this lecture, we will cover mathematical operations, legal variable names, assigning values to variable names, variable types, and some basic input/output functions.

Variables

Variable names can only contain alphanumeric characters and underscores. Variable names may not start with numbers.  Here are a few examples of legal variable names,

legalVariableName
legal_variable_name
variable1

Variable names are case sensitive. Therefore, the following variables would be two distinct variables

Variable1
variable1

Variable Assignment and Reassignment

The value on the right of the expression is assigned to the variable on the left of the equation. In the following expression, the value of 5 is assigned to a.

a = 5

Values stored in a variable can be changed. This is different than the mathematical concept of a variable.

a = 5

Variable Types

Variables are able to store many data types. Three types that we will explore in this lecture are the integer, floating point number, and string.

a = 5
b = 4.0
s = 'hi'

Mathematical Operations

Many of the basic mathematical operators, such as addition, multiplication, subtraction, and division, are built in to Python. Additional operators are available via modules, which we will discuss later.

Basic Input/Output (I/O)

Python provides several methods for receiving input and displaying output from and to the user. Over the course of the semester we will look at several methods such as reading a text file, and image, or using your keyboard and mouse. The input commands provides a mechanism to receive text input from the user. The content is then stored into a variable. Additionally, we can display the contents of a variable using the print command. 