Search this site
Embedded Files
Python Jedi
  • Chapters
    • Fork/Clone
    • 1 Version Control
    • 2 Python Console
    • 3 Input/Output
    • 4 Conditionals
    • 5 Looping
    • 6 Num. Systems
    • 7 Graphics
    • 8 Lists/Strings
    • 9 Functions
    • Prisoner's Dilemma
    • 10 Classes
    • 11 Animation
    • 12 User Control
    • 13 Libraries/Modules
    • 14 Game Design
      • 14 Sprites
      • 14 Shooting
      • 14 Explosions
      • 14 Levels
      • 14 Aiming Trig
      • 14 Walls
      • 14 View Port
      • 14 Grids
    • 15 Data Analysis
Python Jedi
  • Chapters
    • Fork/Clone
    • 1 Version Control
    • 2 Python Console
    • 3 Input/Output
    • 4 Conditionals
    • 5 Looping
    • 6 Num. Systems
    • 7 Graphics
    • 8 Lists/Strings
    • 9 Functions
    • Prisoner's Dilemma
    • 10 Classes
    • 11 Animation
    • 12 User Control
    • 13 Libraries/Modules
    • 14 Game Design
      • 14 Sprites
      • 14 Shooting
      • 14 Explosions
      • 14 Levels
      • 14 Aiming Trig
      • 14 Walls
      • 14 View Port
      • 14 Grids
    • 15 Data Analysis
  • More
    • Chapters
      • Fork/Clone
      • 1 Version Control
      • 2 Python Console
      • 3 Input/Output
      • 4 Conditionals
      • 5 Looping
      • 6 Num. Systems
      • 7 Graphics
      • 8 Lists/Strings
      • 9 Functions
      • Prisoner's Dilemma
      • 10 Classes
      • 11 Animation
      • 12 User Control
      • 13 Libraries/Modules
      • 14 Game Design
        • 14 Sprites
        • 14 Shooting
        • 14 Explosions
        • 14 Levels
        • 14 Aiming Trig
        • 14 Walls
        • 14 View Port
        • 14 Grids
      • 15 Data Analysis

Fork/Clone  Ch.02 Python Console Jedi Training

Interactive Python Console

In this chapter we will be using PyCharm's Interactive Python Console to learn some Python Basics and get some immediate feedback. If you look at the bottom left of the PyCharm window you should see a Python Console tab. When clicked, it should open the Python Console. The three green arrows are where we will enter a line of code.

Commenting

In Python, the # symbol comments out the rest of the line and nothing is executed. Just press enter. You can write a comment at any time during your logging session if you want to take a note or answer a question. Your code throughout the year should be heavily commented. Write a comment similar to the following:

>>> # This is Marc Hermon's Chapter 2 Log

You can use triple quotes (''') before and after multi-line comments. These are usually called doc-strings.

>>> '''This is 

a multi-line

comment called 

a docstring '''

Operators

The console will evaluate expressions, and if you don't tell it to do anything with the expression, it will send the result back to you as output. Try adding two integers:

>>> 5 + 3

Out: 8

Adding two integer types results in an int, which can only represent an integer. It is of type int. A variable is a place in memory to hold data, and a variable's type says what sort of things the bits in memory represent.

If you put a decimal point in a number, that number is identified as the variable type float. Floats have limited precision but can approximate any real number, including integers. Ignore the last few digits if a lot of digits come after the decimal point. What happens if you add a float to an int?

>>> 5 + 3.

Out: 8.0

Below is a table of some common mathematical operators. The floor division will always round the answer down to the nearest integer. Keep in mind that would mean that -7.3 would become -8. Modulus or Mod for short gives the remainder of a division problem instead of the coefficient. Normal mathematical order of operations still apply in coding.

Predict the output of the following in your notebook before you evaluate them.

>>> 7 * 2

>>> 7 * 2.

>>> 7 / 2

>>> 7 / 2.

>>> 10 / 3

>>> 2**3

>>> 8%3

>>> 23**43

You can also call existing functions and they will return a value. Below is the absolute value function. Run it.

>>> abs(-7)

Assigning Variables

You can assign a value to a variable. Python executes the assignment command, but nothing is returned. Only the state is affected: the variable a is now bound to bits in memory that represent the value 2. When you assign a value to a variable, the variable must be on the left side of the = sign. This is one of the syntax rules for Python. The syntax of a language specifies how the symbols of the language are allowed to be put together.

>>> a = 2

>>> b = 7

You can then do math on the assigned variables. Determine the output of the following:

>>> a*b

>>> b**a

>>> b%2

Determine the output of each of the following:

>>> x=8

>>> x

>>> x=x+1

>>> x

>>> x=10

>>> x+=1

>>> x

The x+=1 increments the variable by 1. It is a shorthand way of writing x=x+1. You can also use this with other operators such as x*=3 which is the shorthand way of writing x=x*3.

Unlike most languages, Python allows multiple assignment in a single line of code, as shown here:

>>> x,y= (65,40)

>>> x

>>> y

You can use the + operator to concatenate (join) strings together. What is the result of this code?

>>> word1 = "Hello"

>>> word2 = "World"

>>> phrase = word1 + word2

>>> phrase

Proper Variable Names

Variables should start with a lower case letter. Variables can start with an upper case letter or an underscore, but those are special cases and should not be done on a normal basis. After the first lower case letter, the variable may include uppercase and lowercase letters, along with numbers and underscores. Variables may not include spaces. Variables are case sensitive. The official style guide for Python says that multi-word variable names in Python should be separated by underscores. For example, use force_sensitive and not forceSensitive. All upper-case variable names like MAX_SPEED are allowed only in circumstances where the variable's value should never change. A variable that isn't variable is called a constant. Here are some example variable names that are ok, and not ok to use:

Checking Variable Types

Assign the following four variables as seen below:

>>> a="This is a sentence!"

>>> b=3

>>> c=4.3

>>> d=True

You can now check the type of each variable by using type( ). The first one is demonstrated below and you should do the rest.

>>> type(a)

Out: str

This is a good place to stop the chapter and digest what you've learned so far. 

Quick Lookup:How to download and turn in Jedi Tasks
Syllabus
Marc C. Hermon - Python Jedi InstructorUrbandale High School, Urbandale, Iowahermonm@urbandaleschools.comWebsite: EngineeringU.com
Google Sites
Report abuse
Google Sites
Report abuse