Programming Basics: You need three things…
1. Input - a way for the computer to get the data it will to work with
2. Processing- instructions about what to do with the data
3. Output- a way to communicate the data with the rest of the world
The Python Integrated Development Environment (IDE): IDLE
- You need to write code that a computer will understand
- IDE or Integrated Development Environment is a software application that includes a
source code editor that translates a programmers code into the format readable by a
computer and includes tools to help the programmer write code
IDLE is an easy-to-use IDE that comes packaged with Python
Download and Install Python and IDLE
- To download and install Python on desktop/laptop, go to
https://www.python.org/downloads/ and select your operating system
- You need a version of Python 3 (used since 2008)
- Python 2 is much older and significantly different from Python 3
- Save the executable file that is downloaded and click on Install
- In pop up window, click Install Now
- To create programs using Python, you need the IDE named IDLE.
This comes with the Python download, but you want easy access
to IDLE (Icon pictured on right)
The IDLE Shell and the IDLE Editor
- IDLE is made up of 1) an interactive shell and 2) a text editor
- Interactive shell- makes it easy to enter and test Python
statements without writing a whole program
- Text editor- use to write longer programs
- When you open IDLE. it opens the interactive shell to type code.
- Enter your code after the >>> prompt, and press Enter
IDLE Shell Example
, - In this example, the user types 3 * 8 and pressed Enter (The result, 24, is displayed)
- Next the user used Print () function, that tells Python to display the what is inside the
parentheses
- After typing print(“Python is fun!”) and pressing Enter, the output is the text,
Python is fun!
- Finally, the user entered an equation requiring a calculation on one line (x = 73 * 4) a
print() command on the next line, and the result was the required text as well as the
result of the calculation
Using the IDLE editor
- Interactive shell makes it easy to experiment with Python code, allowing you to see the
results immediately
- Editor is used to write the entire program you need, not just the segment from the shell
- Python code cannot be written in a word processing program like Word
- IDLE editor works like a simply word processor and has similar features like File, Edit,
and Format menus, but allows to run Python source code
- Once a program is created in the editor and run, the program will run in the shell
IDLE editor features
- Highlights syntax errors (errors occurring from not following Python rules)
- Tells you any runtime errors (errors that occur when a Python statement cannot be
executed as the program is running)
EXAMPLE of IDLE Editor
Photo shows the code for a short program that asks the user to enter their first name and last
initial, then uses that info to create an email address for a college with that email extension
(pythoncollege.edu). In this example, the user’s name is Andy Morris.
Editor is on the left, shell is on the right
Python Basics
- When you run a program, you see the results of your code
- A program needs input, instructions on what to do, and the output
Input
- Input operation- program that takes data from outside source like the program’s user,
Internet, or a file
- When input is required, the program has a prompt that tells the user what is needed
- In Python, a prompt takes the form of an input request, usually stored in a variable
, - Python syntax:
- Variable_name = input (“instruction to user about what to enter”)
Example- program asking the user to enter their name. That value is stored in a variable
named user_name
- User_name = input (“Enter your name: ”)
- Take note of the space between the colon and the ending quotation mark
- Everything within quotes will be displayed when the program runs
- When the prompt has no space after the colon:
input(“Enter your name:”)
Enter your name:Andy
- When the prompt has a space coded before the closing quotation mark:
input(“Enter your name:”)
Enter your name: Andy
Variables
- At least one variable is needed in every program
- Variable- name for a storage location in a computers memory
- Can change value as the program runs
- Value of a variable- what is stored in that location
Example of changing the value of a variable:
Here, there are four variables: num_1, num_2, num_3, and sum
- On line 3, the value of sum is equal to the sum of the values num_1 and num_2, which is
8
- On line 6, the value of sum changes because now it contains the old value (8) PLUS the
value of num_3. The variable sum now has the value of 17
Example of joining variables: