Python 3 study notes

by Michaelis

Identifiers

  • Used to describe objects in Python.

  • Must begin with a letter or underscore.

  • Case sensitive.

    FooBar != foobar

  • Can't use reserved words by python.

Name Error

Error when you try to use an identifier without a value assigned to it.

Expressions

  • + - * /

  • ** % // Exponentiate, Modulus(remainder), Floor Division(quotiont only).

  • () gives precedence(order) in witch operations are computed.

Statements

  • Statements are lines of code that 'do something'.

  • Output Statements: print, etc.

  • Assignment Statements: <variable> = <expressions>, user input, etc.

Difinite Loops

for <var> in <sequence>:
    <body>
  • beginning and end are indicated.

  • the for loop is a control structure.

Data types

  • Use type() to see the data type of a variable

The String Date Type

  • string: str

  • "This is a string"

  • index start with 0 from the left side, -1 on the right side.

  • Use str[] to index

  • Use str[0:n] to slice.

  • Use + to concatenate("glues"), * concatenates itself.

  • len("str") give you the length of the string

  • for <var> in <string> iterates though characters.

  • In ASCII system, ord() returns numeric code for character, chr() converts numeric code to character.

string methods
  • s.capitalize() First characters will be capitalized.

  • s.title() First character in each words will be capitalized.

  • s.center(width) Center it in a field of given width.

  • s.count(sub) Count the number of occurrences of sub in s.

  • s.find(sub) Find the first position where sub occurs in s.

  • s.join(list) Concatenate list of strings into one large string using s as separator.

  • s.ljust(width) Like center, but s is left-justified.

  • s.rstrip() Copy of s with trailing whitespace removed

  • s.split() Split s into a list of substrings.

  • s.upper() all haracters converted to uppercase.

Input/output as string manipulation
  • s.split("") split where the thing in "" are separators.

  • str(<expr>) returns a string representation of <expr>.

  • eval(<string>) evaluates <string> as an expression.

Lists and Sequences
  • Lists are mutable, meaning they can be changed

  • Strings can not be changed

String Formatting
  • <template-string>.format(<values>)

  • <index>:<format-specifier>

Numeric data types

  • integer: int, convert to integer: int()

  • decimal numbers: float, convert to float: float()

Logic

  • == != < > <= >=

  • and or not

Name spaces

  • different objects can't be given the same name

Import Library

  • import <lib> or from <lib> import <function>

Range

  • range(0,n+1) is actually 0 to n.

Graph

  • import graphics.py

  • Data inside an object is called a data attribute

  • Functions and procedures inside the object are called methods

File Processing

  • <filevar> = open(<name>, <mode>)

  • <file>.read() returns the entire remaining contents of the file as a single (possibly large, multi-line) string.

  • <file>.readline() returns the next line of the file. This is all text up to and including the next newline character.

  • <file>.readlines() returns a list of the remaining lines in the file. Each list item is a single line including the newline characters.

Functions

  • The part of the program that creates a function is called a function definition

  • When the function is used in a program, we say the definition is called or invoked

  • These two routines could be collapsed together by using a parameter.

  • The scope of a variable refers to the places in a program a given variable can be referenced

  • Formal parameters, like all variables used in the function, are only accessible in the body of the function.

  • The formal parameters of the function get assigned the values supplied by the actual parameters in the call.

  • Un-named parameters discussed earlier are called positional parameters.

  • The parameters have to be in order!

  • Named parameters: parameters can be explicitly assigned a value by name, these are often used to implement default, or optional, values.

  • the formal parameters of a function only receive the values of the actual parameters. The function does not have access to the variable that holds the actual parameter. Python is said to pass parameters by object reference.

None

  • The sole value of the type NoneType, frequently used to represent the absence of a value.