Python Programming/Interactive mode - Wikibooks, open books for an open world (2024)


Python has two basic modes: script and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.

Interactive mode is a good way to play around and try variations on syntax.

On macOS or linux, open a terminal and simply type "python". On Windows, bring up the command prompt and type "py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu. IDLE is a GUI which includes both an interactive mode and options to edit and run files.

Python should print something like this:

$ pythonPython 3.0b3 (r30b3:66303, Sep 8 2008, 14:01:02) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>

(If Python doesn't run, make sure it is installed and your path is set correctly. See Getting Python.)

The >>> is Python's way of telling you that you are in interactive mode. In interactive mode what you type is immediately run. Try typing 1+1 in. Python will respond with 2. Interactive mode allows you to test out and see what Python will do. If you ever feel the need to play with new Python statements, go into interactive mode and try them out.

A sample interactive session:

>>> 55>>> print(5*7)35>>> "hello" * 2'hellohello'>>> "hello".__class__<type 'str'>

However, you need to be careful in the interactive environment to avoid confusion. For example, the following is a valid Python script:

if 1: print("True")print("Done")

If you try to enter this as written in the interactive environment, you might be surprised by the result:

>>> if 1:... print("True")... print("Done") File "<stdin>", line 3 print("Done") ^SyntaxError: invalid syntax

What the interpreter is saying is that the indentation of the second print was unexpected. You should have entered a blank line to end the first (i.e., "if") statement, before you started writing the next print statement. For example, you should have entered the statements as though they were written:

if 1: print("True") print("Done")

Which would have resulted in the following:

>>> if 1:... print("True")...True>>> print("Done")Done>>>

Interactive mode[edit | edit source]

Instead of Python exiting when the program is finished, you can use the -i flag to start an interactive session. This can be very useful for debugging and prototyping.

python -i hello.py

For i in range(-1,-5,-1):

 print(i)
Python Programming/Interactive mode - Wikibooks, open books for an open world (2024)
Top Articles
Most Popular Databases in 2020: Here's How They Stack Up - Ormuco
Difference b/w Microsoft Dynamics NAV and Business Central
My Arkansas Copa
Midflorida Overnight Payoff Address
Coffman Memorial Union | U of M Bookstores
Google Jobs Denver
How do you mix essential oils with carrier oils?
270 West Michigan residents receive expert driver’s license restoration advice at last major Road to Restoration Clinic of the year
Autozone Locations Near Me
4156303136
Find your energy supplier
What’s the Difference Between Cash Flow and Profit?
All Buttons In Blox Fruits
Belly Dump Trailers For Sale On Craigslist
Colts Snap Counts
The Superhuman Guide to Twitter Advanced Search: 23 Hidden Ways to Use Advanced Search for Marketing and Sales
Mile Split Fl
Finger Lakes Ny Craigslist
Telegram Scat
Highland Park, Los Angeles, Neighborhood Guide
Georgia Vehicle Registration Fees Calculator
How Much Is Tay Ks Bail
Curry Ford Accident Today
Pinellas Fire Active Calls
Little Caesars 92Nd And Pecos
Is A Daytona Faster Than A Scat Pack
Between Friends Comic Strip Today
The Old Way Showtimes Near Regency Theatres Granada Hills
Engineering Beauties Chapter 1
Weve Got You Surrounded Meme
Tracking every 2024 Trade Deadline deal
Little Einsteins Transcript
Kamzz Llc
APUSH Unit 6 Practice DBQ Prompt Answers & Feedback | AP US History Class Notes | Fiveable
Devotion Showtimes Near The Grand 16 - Pier Park
Manuel Pihakis Obituary
Nextdoor Myvidster
Linabelfiore Of
Personalised Handmade 50th, 60th, 70th, 80th Birthday Card, Sister, Mum, Friend | eBay
Zero Sievert Coop
Quake Awakening Fragments
Blackwolf Run Pro Shop
Keir Starmer looks to Italy on how to stop migrant boats
Shoecarnival Com Careers
Ups Authorized Shipping Provider Price Photos
855-539-4712
tampa bay farm & garden - by owner "horses" - craigslist
Obituary Roger Schaefer Update 2020
Sunset On November 5 2023
Wayward Carbuncle Location
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5555

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.