CIS 111 Comments, literals, variables, numbers, operators, expressions

Objectives

  • create comments in a Python program
  • discuss some of the common data types in Python
  • explain what literals are
  • explain what identifiers, variables, and constants are
  • list the Python operators
  • create expressions in Python

Comments

  • Comments are not executable code. They are for the programmer to read.
  • Comments start with a # sign and continue to the end of the line.
  • There are no multiline comments in Python - but DocStrings are similar.
  • A DocString starts and ends with triple quotes.
  • A DocString should immediately follow a function or class declaration.
  • A DocString is accessible via the __doc__ property of a class or function.

Data types

The term "data type" refers to what type of data a variable, constant, or literal refers to. Some of the more commonly used data types in Python are:

  • strings
  • byte strings/sequences
  • byte arrays
  • integers
  • longs
  • floats
  • complex numbers
  • lists
  • sets
  • frozen sets
  • tuples
  • dicts
  • Booleans

Literals

Literals are explicit values in the source code, such as strings, numbers, etc. Literals can not be renamed or changed. Examples:

  • "Hello, world" (a string literal)
  • 'Hello, world' (a string literal)
  • b'Hello, world' (a byte string literal)
  • u"Hello, world" (a Unicode string literal)
  • 5.2 (a float literal)
  • 5 (an int literal)
  • 2+3j (a complex number literal)
  • [2, 4, 6, 8, 10] (a list literal)
  • {2, 4, 6, 8, 10} (a set literal)
  • (5.10) (a tuple literal)
  • {'hours': 10.5} (a dict literal)
  • True, False (Boolean literals)
  • None

Identifiers, variables, constants

Variables are memory locations that are used to store data. These locations are given names so you can work with them more easily. Modules within programs may also be given names. These names are called identifiers. You get to choose the names for identifiers. Naming rules vary between languages, but some common rules are:

  • names should start with a letter or underscore
  • names may contain only letters, digits, or underscores
  • names may be case sensitive (usually are in modern compilers)
  • some names are reserved by the programming language and you can't use them
  • names should be descriptive and help indicate their purpose
  • names are often "camel-cased" to make them more readable for programmers

Constants are also memory locations that are used to store data. The difference between variables and constants is that constants do not change value once they are set. Variables can be modified.

Reserved words

Reserved words in Python 3.6:

andasassertbreakclass
continuedefdelelifelse
exceptFalsefinallyforfrom
globalifimportinis
lambdaNonenonlocalnotor
passraisereturnTruetry
whilewithyield 

Operators

The following are operators in Python 3.6:

+-***///%@<<>>
&|^~<><=>===!=

Expressions

Expressions are pieces of code that can be evaluated to get a value. Some examples are:

  • "Hello"
  • 5 * 37
  • 3.1415926 * radius ** 2

Sample program using covered concepts

Flowchart:

Flowchart for CalcPay program

Psuedocode:

    get hours and rate
    gross = hours * rate
    display gross
      

Python program:

    hours = float(input("Enter hours: "))
    rate = float(input("Enter rate: "))
    gross = hours * rate
    print("Total: ", gross)