Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 4 – Writing and Executing Python Code

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 4 – Writing and Executing Python Code

Spread the love

Python code can be written and executed in:

– the Python interactive shell

– the Python interactive window

– a file with the .py extension created in your editor or IDE

The Python Interactive Shell

The Python interactive shell stands between the commands or actions and their execution. It waits for the user’s commands, executes them and returns the result of the execution.

In your cmd window you can invoke the Python interpreter like so:

C:\Users\user-1>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Here, in the first line you can see the version of Python (3.7.0). The interpreter is started.

>>> is the command prompt. You can type anything here. Let’s type x:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

We get an error with explanation: the name ‘x’ is unknown.

Now let’s use a valid Python command. Let’s make Python print the standard “Hello World” text:

>>> print("Hello World")
Hello World

Works fine.

Let’s make a quick calculation:

>>> 5 + 3
8

Works fine, too.

When done with the Python Shell, let’s exit it. What we need is the exit() command:

>>> exit()
C:\Users\user-1>

Interactive Mode

You can work in interactive mode in your IDE. So, open the Python interactive window and type:

>>> 5 + 6
11

So, here we have the same command prompt (>>>).

After we enter the statement and press Enter, we can’t go back to the statement and edit it. So, if you want to make a change, there are 3 ways of doing it:

1) You can type the statement again – it’s not a big deal with short statements like the one above.

2) You can copy and paste the statement.

3) You can recover the statement from the command history by hitting the up arrow key – it’s a time-saving feature.

You can leave the interactive session at any time.

Python Files

To save and edit our program in a file we need an editor.

Let’s write a short program in the editor:

print("Hello")

and save it as welcome.py. The .py extension is characteristic for Python code.

To run the program in the IDE just run it directly there. We can go to Debug -> Start Debugging, press the Run button or press F5 in Visual Studio. If you’re using a different IDE, there may be other options as well.

To run the program in command line, first navigate to the folder where the file is saved:

d:\>cd D:\code

and then run the python interpreter and write the name of your file:

D:\code>python welcome.py
Hello
D:\code>

As you can see, the output is printed to the console.

Python as a Calculator

We can use Python in interactive mode as a calculator. We use the standard mathematical operations: + – * / and ** for exponentiation. We call whole numbers (positive, negative and 0) integers, for example 5, 8, 0, -13, -500. Numbers with a decimal point like 1.23, -0.004, 6.1 are called floating-point numbers of floats for short.

Python as a Calculator

We can use literals in operations:

>>> 5 + 3
8
>>> 4 - 2
2
>>> 8 * 2
16
>>> 7 / 4
1.75
>>> 2 ** 5
32

We can use the underscore sign “_” in our code. It keeps the most recent output value:

>>> 5 ** 3
125
>>> 4 * _  # now _ keeps the value 125
500
>>> _ - 100  # now _ keeps the value 500
400

We can use the underscore variable only in interactive mode.

QUIZ

1. In your cmd window you can invoke the Python interpreter like so:
    A) C:\Users\user-1>.python 3
    B) C:\Users\user-1>.python exe
    C) C:\Users\user-1>python
 
2. The default command prompt is:
    A) #
    B) >>>
    C) >
 
3. In order to exit the Python Shell we need to type:
    A) >>> exit()
    B) >>> exit
    C) >>> exit python
 
4. You can recover the statement from the command history in interactive mode by pressing:
    A) Enter
    B) spacebar
    C) the up arrow
 
5. You can use interactive mode:
    A) only in the Python Shell
    B) only in your IDE
    C) in both the Python Shell and your IDE
 

TRUE OR FALSE?

1) After we enter a statement in interactive mode and press Enter, we can go back to the statement and edit it.
2) You can copy and paste a statement in interactive mode.
3) To run a Python program in command line, we should first navigate to the folder where the file is saved.
4) In Visual Studio you can run the code by pressing F5:
5) Integer numbers include only positive whole numbers.
6) Floating point numbers are numbers with the decimal point and decimal digits.
7) We can use the underscore variable only in interactive mode.

WHAT’S THE OUTPUT?

1)
>>> print(“hi”)
 
2)
>>> 2 ** 3
 
3)
>>> 6 * 5 30 >>> 5 + _  

SOLUTION

QUIZ

1. In your cmd window you can invoke the Python interpreter like so:
    A) C:\Users\user-1>.python 3
    B) C:\Users\user-1>.python exe
    C) C:\Users\user-1>python
 
2. The default command prompt is:
    A) #
    B) >>>
    C) >
 
3. In order to exit the Python Shell we need to type:
    A) >>> exit()
    B) >>> exit
    C) >>> exit python
 
4. You can recover the statement from the command history in interactive mode by pressing:
    A) Enter
    B) spacebar
    C) the up arrow
 
5. You can use interactive mode:
    A) only in the Python Shell
    B) only in your IDE
    C) in both the Python Shell and your IDE
 

TRUE OR FALSE?

1) After we enter a statement in interactive mode and press Enter, we can go back to the statement and edit it.
False
2) You can copy and paste a statement in interactive mode.
True
3) To run a Python program in command line, we should first navigate to the folder where the file is saved.
True
4) In Visual Studio you can run the code by pressing F5:
True
5) Integer numbers include only positive whole numbers.
False
6) Floating point numbers are numbers with the decimal point and decimal digits.
True
7) We can use the underscore variable only in interactive mode.
True

WHAT’S THE OUTPUT?

1)
>>> print(“hi”)  
Output:
hi  
 
2)
>>> 2 ** 3  
Output:
8  
 
3)
>>> 6 * 5 30 >>> 5 + _  
Output:
35  

PROJECT

'''
Writing and Executing Python Code - Project

CALCULATOR
__________________________________________________________

Follow the steps below. You can find the solution at the bottom of this 
document. 

Code that is enclosed between triple quotes like this one is treated as
a comment and is ignored. Also code that follows the # character is
a comment. 

#1. Open an interactive session in the Python Interactive Shell.
#2. Perform the following calculations:

36 / 6
4 * 8
120 + 50
45 - 90

#3. When done, exit from the Python Interactive Shell.

'''

PROJECT SOLUTION

'''
#1. Open the console (e.g. by searching for cmd in Windows) and type:
x>python

x stands for the location where your Python interpreter is installed.

This is what you will get:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

What you actually get depends on your system and version of Python you use.


#2. Type the calculations directly in the window:

>>> 36 / 6
6.0
>>> 4 * 8
32
>>> 120 + 50
170
>>> 45 - 90
-45


#3. Type exit() and hit Enter.

'''


Spread the love

Leave a Reply