Python Keywords, Concepts, Functions

From Computer Science
Revision as of 17:04, 18 January 2020 by Jkinne (talk | contribs) (Concepts)
Jump to: navigation, search

See also Python Programming - Getting Started

Following are terse descriptions for Python3 keywords, programming concepts, and commonly used functions. For more information, see w3schools for a bit more explanation and python.org for the language reference.

Keywords

The following are heavily used in all levels of Python programming. This is a minimum set of keywords to know how to use.

  • break - exit the current loop
  • continue - in a loop, go to next iteration of the loop
  • is - determine whether two objects are the same object (not just value)
  • import, from - to load a module
  • def - declare a function
  • while - loop with only condition
  • if, elif, else - conditional statements (three keywords)
  • for - loop with that iterates through a list
  • return - keyword to send a value back from a function
  • and - boolean operator, True only if both sides are True
  • or - boolean operator, True if either side is True
  • not - boolean operator, negates
  • True, False - boolean values (two keywords)
  • del - remove from a list by position
  • try, except - handle an exception, basic use (2 keywords)
  • raise, assert - raise/indicate an exception (2 keywords)
  • in - test if something is inside of a list/string/tuple/dictionary
  • None - special value for a variable that has no value
  • pass - empty statement that does not do anything

More Keywords

These keywords are often not introduced or heavily used until the second Python course.

  • as, finally, with, else - exception handling, more keywords (4 keywords)
  • class - defining new class data type (for object-oriented programming)
  • lambda - create unnamed / anonymous function
  • global, nonlocal - access variables outside of current scope (2 keywords)
  • async, await - for writing asynchronous code with asynchio package (2 keywords)
  • yield - for creating generator functions

Concepts

See this video for a demo and explanation of basic data types and variables.

These are terms that we use to describe programs. These are terms that have a precise meaning when talking about programs.

  • string - text data type
  • boolean - data type for True and False
  • floating point - data type that stores numbers with fractional parts (e.g., 3.14 or 2.2)
  • integer - data type that stores only integers
  • None - a special value in Python that means "nothing" but is different than 0, False, and ""
  • variable - name for a place in memory to store values. Two basic things you do with a variable - (i) store a value into the variable, (ii) get the value from the variable.
  • keywords - also called reserved words - these are names that should not be used for variable names because they have special meaning to python. Example: for is used for loops and shouldn't be used as a variable name.
  • function - also called procedures or methods - name for a block of code that does something and that your code can use when needed. Two basic things you can do with a function - (i) define what the function is (specify the code for the function), (ii) call the function later on in your code. Three main parts of defining function - (a) function code (called the body), (b) function parameters (aka inputs), (c) function return values. Three main parts of calling/using a function - (1) specify the parameters, (2) call the function, (3) get the return value of the function.

Commonly Used Functions

  • print - function to write to the screen
  • int, float, str - functions to convert to integer, floating point number, or string
  • range - function to generate a sequence of numbers
  • len - function to get the length of a string, list, or tuple