How to Run Function Again From Python Shell
Lookout man Now This tutorial has a related video course created by the Existent Python team. Lookout man it together with the written tutorial to deepen your agreement: Starting With Python IDLE
If you've recently downloaded Python onto your computer, then you may take noticed a new plan on your machine called IDLE. You might exist wondering, "What is this program doing on my computer? I didn't download that!" While you may not accept downloaded this program on your ain, IDLE comes bundled with every Python installation. It's at that place to help y'all get started with the language right out of the box. In this tutorial, you'll acquire how to work in Python IDLE and a few cool tricks yous can use on your Python journey!
In this tutorial, you'll larn:
- What Python IDLE is
- How to interact with Python directly using IDLE
- How to edit, execute, and debug Python files with IDLE
- How to customize Python IDLE to your liking
What Is Python IDLE?
Every Python installation comes with an Integrated Development and Learning Environment, which you'll encounter shortened to IDLE or even IDE. These are a form of applications that help you write code more efficiently. While in that location are many IDEs for you to cull from, Python IDLE is very bare-basic, which makes it the perfect tool for a offset programmer.
Python IDLE comes included in Python installations on Windows and Mac. If yous're a Linux user, then y'all should exist able to observe and download Python IDLE using your parcel managing director. Once you've installed it, y'all tin so use Python IDLE as an interactive interpreter or as a file editor.
An Interactive Interpreter
The best place to experiment with Python lawmaking is in the interactive interpreter, otherwise known equally a beat out. The beat out is a basic Read-Eval-Print Loop (REPL). It reads a Python statement, evaluates the result of that statement, and and then prints the result on the screen. Then, information technology loops back to read the adjacent statement.
The Python shell is an first-class identify to experiment with small code snippets. You lot can access it through the last or command line app on your machine. You can simplify your workflow with Python IDLE, which volition immediately start a Python shell when yous open information technology.
A File Editor
Every programmer needs to exist able to edit and save text files. Python programs are files with the .py
extension that incorporate lines of Python code. Python IDLE gives you lot the ability to create and edit these files with ease.
Python IDLE also provides several useful features that you lot'll meet in professional IDEs, similar bones syntax highlighting, code completion, and auto-indentation. Professional IDEs are more robust pieces of software and they accept a steep learning bend. If you're just beginning your Python programming journey, and so Python IDLE is a bully culling!
How to Employ the Python IDLE Shell
The shell is the default mode of operation for Python IDLE. When y'all click on the icon to open the program, the shell is the beginning thing that y'all come across:
This is a blank Python interpreter window. You can employ information technology to kickoff interacting with Python immediately. You can test information technology out with a curt line of code:
Here, you used print()
to output the string "Hello, from IDLE!"
to your screen. This is the about basic way to interact with Python IDLE. You type in commands 1 at a fourth dimension and Python responds with the result of each command.
Side by side, accept a look at the card bar. You lot'll see a few options for using the shell:
You can restart the crush from this menu. If yous select that option, so you'll clear the state of the shell. It will human activity every bit though you lot've started a fresh instance of Python IDLE. The shell volition forget about everything from its previous land:
In the image above, you starting time declare a variable, x = 5
. When you lot call impress(x)
, the shell shows the correct output, which is the number 5
. However, when you restart the beat out and try to telephone call print(10)
again, y'all can encounter that the beat out prints a traceback. This is an error message that says the variable ten
is not defined. The shell has forgotten about everything that came before information technology was restarted.
You can also interrupt the execution of the shell from this menu. This will stop any programme or argument that'south running in the trounce at the time of interruption. Take a look at what happens when you send a keyboard interrupt to the shell:
A KeyboardInterrupt
error bulletin is displayed in ruby-red text at the bottom of your window. The program received the interrupt and has stopped executing.
How to Piece of work With Python Files
Python IDLE offers a full-fledged file editor, which gives you the ability to write and execute Python programs from within this program. The built-in file editor also includes several features, like code completion and automated indentation, that will speed up your coding workflow. First, let's take a await at how to write and execute programs in Python IDLE.
Opening a File
To start a new Python file, select File → New File from the carte bar. This volition open a blank file in the editor, like this:
From this window, you tin write a brand new Python file. You tin too open an existing Python file by selecting File → Open… in the menu bar. This will bring up your operating system'south file browser. Then, you can find the Python file you want to open up.
If you lot're interested in reading the source code for a Python module, so you tin select File → Path Browser. This volition let you view the modules that Python IDLE can see. When you lot double click on one, the file editor will open up and you'll exist able to read it.
The content of this window volition be the aforementioned as the paths that are returned when yous call sys.path
. If you know the name of a specific module you desire to view, then you lot can select File → Module Browser and type in the name of the module in the box that appears.
Editing a File
One time you've opened a file in Python IDLE, yous tin can so brand changes to information technology. When you're fix to edit a file, you'll run across something like this:
The contents of your file are displayed in the open up window. The bar along the top of the window contains three pieces of of import information:
- The name of the file that yous're editing
- The total path to the folder where yous tin find this file on your reckoner
- The version of Python that IDLE is using
In the epitome above, you're editing the file myFile.py
, which is located in the Documents
folder. The Python version is 3.vii.1, which you tin come across in parentheses.
There are as well two numbers in the bottom right corner of the window:
- Ln: shows the line number that your cursor is on.
- Col: shows the column number that your cursor is on.
It's useful to encounter these numbers so that you lot can notice errors more quickly. They also help you brand sure that y'all're staying within a certain line width.
There are a few visual cues in this window that will help you remember to save your work. If you lot look closely, then you'll see that Python IDLE uses asterisks to permit yous know that your file has unsaved changes:
The file name shown in the height of the IDLE window is surrounded by asterisks. This means that there are unsaved changes in your editor. You tin relieve these changes with your system'southward standard keyboard shortcut, or yous tin select File → Relieve from the menu bar. Brand certain that you save your file with the .py
extension and then that syntax highlighting volition be enabled.
Executing a File
When you desire to execute a file that you've created in IDLE, you should first brand certain that information technology's saved. Remember, yous can run across if your file is properly saved by looking for asterisks around the filename at the top of the file editor window. Don't worry if you forget, though! Python IDLE will remind you to save whenever you effort to execute an unsaved file.
To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the card bar. Either option will restart the Python interpreter and and then run the code that you've written with a fresh interpreter. The process is the same equally when you run python3 -i [filename]
in your terminal.
When your code is done executing, the interpreter will know everything about your code, including any global variables, functions, and classes. This makes Python IDLE a neat place to audit your data if something goes wrong. If you e'er need to interrupt the execution of your programme, then you can press Ctrl + C in the interpreter that's running your code.
How to Meliorate Your Workflow
Now that you've seen how to write, edit, and execute files in Python IDLE, information technology's fourth dimension to speed upwardly your workflow! The Python IDLE editor offers a few features that yous'll see in most professional IDEs to help you code faster. These features include automatic indentation, code completion and phone call tips, and lawmaking context.
Automatic Indentation
IDLE will automatically indent your code when it needs to start a new cake. This usually happens after you lot blazon a colon (:
). When y'all hit the enter key subsequently the colon, your cursor will automatically movement over a certain number of spaces and begin a new code block.
You can configure how many spaces the cursor will move in the settings, but the default is the standard four spaces. The developers of Python agreed on a standard way for well-written Python code, and this includes rules on indentation, whitespace, and more than. This standard style was formalized and is now known equally PEP eight. To larn more about it, check out How to Write Beautiful Python Code With PEP 8.
Code Completion and Call Tips
When yous're writing code for a large project or a complicated problem, you tin spend a lot of time just typing out all of the code yous need. Code completion helps yous save typing time by trying to finish your lawmaking for you lot. Python IDLE has bones code completion functionality. It can but autocomplete the names of functions and classes. To utilize autocompletion in the editor, just press the tab key subsequently a sequence of text.
Python IDLE volition also provide call tips. A call tip is similar a hint for a sure part of your code to help you remember what that element needs. Later you type the left parenthesis to brainstorm a function call, a call tip will appear if y'all don't type anything for a few seconds. For example, if you can't quite remember how to suspend to a list, and so you tin can suspension after the opening parenthesis to bring up the telephone call tip:
The telephone call tip will display as a popup note, reminding you how to append to a list. Telephone call tips like these provide useful information as you're writing code.
Lawmaking Context
The code context functionality is a neat characteristic of the Python IDLE file editor. Information technology will show you lot the scope of a function, class, loop, or other construct. This is peculiarly useful when you lot're scrolling through a lengthy file and need to keep track of where you are while reviewing lawmaking in the editor.
To turn information technology on, select Options → Lawmaking Context in the menu bar. Yous'll run into a gray bar announced at the top of the editor window:
Every bit you lot ringlet downward through your lawmaking, the context that contains each line of code will stay inside of this gray bar. This means that the print()
functions yous see in the paradigm to a higher place are a part of a main function. When y'all achieve a line that's exterior the telescopic of this function, the bar will disappear.
How to Debug in IDLE
A bug is an unexpected trouble in your program. They can announced in many forms, and some are more difficult to fix than others. Some bugs are tricky enough that you won't exist able to catch them past just reading through your program. Luckily, Python IDLE provides some basic tools that will help yous debug your programs with ease!
Interpreter DEBUG Mode
If you lot want to run your lawmaking with the born debugger, and so you'll need to turn this feature on. To exercise so, select Debug → Debugger from the Python IDLE menu bar. In the interpreter, yous should come across [DEBUG ON]
appear but before the prompt (>>>
), which means the interpreter is set and waiting.
When yous execute your Python file, the debugger window volition announced:
In this window, you can inspect the values of your local and global variables equally your lawmaking executes. This gives you insight into how your information is being manipulated every bit your code runs.
Yous can too click the following buttons to move through your lawmaking:
- Go: Printing this to advance execution to the next breakpoint. You lot'll acquire near these in the next department.
- Footstep: Press this to execute the current line and go to the next i.
- Over: If the electric current line of code contains a function call, and then press this to step over that function. In other words, execute that part and go to the adjacent line, merely don't pause while executing the function (unless at that place is a breakpoint).
- Out: If the current line of lawmaking is in a office, then printing this to footstep out of this function. In other words, proceed the execution of this function until you return from it.
Be careful, because there is no opposite push! You can only step forward in time through your programme's execution.
You'll besides run into four checkboxes in the debug window:
- Globals: your program'south global information
- Locals: your program'due south local data during execution
- Stack: the functions that run during execution
- Source: your file in the IDLE editor
When you select 1 of these, you'll see the relevant information in your debug window.
Breakpoints
A breakpoint is a line of code that you've identified every bit a place where the interpreter should intermission while running your lawmaking. They will only piece of work when DEBUG fashion is turned on, so make sure that yous've done that first.
To set a breakpoint, right-click on the line of code that you wish to pause. This will highlight the line of code in yellow as a visual indication of a set up breakpoint. Yous can ready as many breakpoints in your code equally you lot similar. To undo a breakpoint, right-click the aforementioned line once again and select Articulate Breakpoint.
Once you've set your breakpoints and turned on DEBUG manner, you can run your code as you would normally. The debugger window will pop up, and you can kickoff stepping through your code manually.
Errors and Exceptions
When you encounter an fault reported to yous in the interpreter, Python IDLE lets you jump right to the offending file or line from the menu bar. All you have to exercise is highlight the reported line number or file proper noun with your cursor and select Debug → Go to file/line from the menu bar. This is will open up upwardly the offending file and take yous to the line that contains the error. This characteristic works regardless of whether or non DEBUG way is turned on.
Python IDLE also provides a tool called a stack viewer. You can access it under the Debug pick in the carte bar. This tool volition show you the traceback of an error as it appears on the stack of the last error or exception that Python IDLE encountered while running your code. When an unexpected or interesting fault occurs, you might detect information technology helpful to take a await at the stack. Otherwise, this feature can be difficult to parse and likely won't be useful to you unless you lot're writing very complicated code.
How to Customize Python IDLE
There are many ways that you can give Python IDLE a visual style that suits you. The default expect and feel is based on the colors in the Python logo. If you don't similar how anything looks, then y'all can about ever change information technology.
To access the customization window, select Options → Configure IDLE from the bill of fare bar. To preview the result of a change y'all want to make, press Utilise. When you're done customizing Python IDLE, press OK to relieve all of your changes. If you don't want to save your changes, then simply press Cancel.
At that place are five areas of Python IDLE that you can customize:
- Fonts/Tabs
- Highlights
- Keys
- General
- Extensions
Let's have a expect at each of them at present.
Fonts/Tabs
The first tab allows you to modify things like font colour, font size, and font mode. Y'all tin change the font to almost whatsoever manner you like, depending on what'south bachelor for your operating organisation. The font settings window looks like this:
You tin utilise the scrolling window to select which font yous adopt. (I recommend yous select a fixed-width font like Courier New.) Pick a font size that's large plenty for you to encounter well. You can also click the checkbox next to Bold to toggle whether or not all text appears in assuming.
This window volition too allow you lot modify how many spaces are used for each indentation level. Past default, this volition be ready to the PEP 8 standard of 4 spaces. You tin change this to make the width of your code more than or less spread out to your liking.
Highlights
The second customization tab volition permit you change highlights. Syntax highlighting is an important feature of any IDE that highlights the syntax of the language that you're working in. This helps you visually distinguish betwixt the different Python constructs and the data used in your lawmaking.
Python IDLE allows you to fully customize the appearance of your Python code. It comes pre-installed with three different highlight themes:
- IDLE Twenty-four hours
- IDLE Night
- IDLE New
You tin select from these pre-installed themes or create your own custom theme correct in this window:
Unfortunately, IDLE does not allow you to install custom themes from a file. You have to create customs theme from this window. To do so, you can but start irresolute the colors for different items. Select an detail, and then press Choose color for. You'll be brought to a color picker, where you tin can select the exact colour that you want to utilise.
You'll and so be prompted to salve this theme equally a new custom theme, and you lot can enter a proper name of your choosing. You tin can so continue changing the colors of different items if yous'd similar. Remember to press Utilise to run into your changes in action!
Keys
The third customization tab lets you map dissimilar fundamental presses to actions, also known as keyboard shortcuts. These are a vital component of your productivity whenever you use an IDE. You can either come with your ain keyboard shortcuts, or you can use the ones that come with IDLE. The pre-installed shortcuts are a good place to get-go:
The keyboard shortcuts are listed in alphabetical order past action. They're listed in the format Action - Shortcut, where Activeness is what will happen when you press the primal combination in Shortcut. If yous want to use a built-in key set up, then select a mapping that matches your operating system. Pay close attention to the different keys and brand sure your keyboard has them!
Creating Your Own Shortcuts
The customization of the keyboard shortcuts is very similar to the customization of syntax highlighting colors. Unfortunately, IDLE does not let you lot to install custom keyboard shortcuts from a file. You lot must create a custom fix of shortcuts from the Keys tab.
Select one pair from the list and press Get New Keys for Option. A new window will pop up:
Here, you tin use the checkboxes and scrolling carte du jour to select the combination of keys that yous desire to use for this shortcut. Y'all can select Advanced Key Binding Entry >> to manually type in a command. Annotation that this cannot pick upward the keys you lot press. You accept to literally blazon in the command as you lot run across information technology displayed to y'all in the list of shortcuts.
General
The fourth tab of the customization window is a place for modest, full general changes. The full general settings tab looks like this:
Here, you can customize things like the window size and whether the shell or the file editor opens beginning when you lot start Python IDLE. Nigh of the things in this window are not that exciting to change, so yous probably won't demand to fiddle with them much.
Extensions
The 5th tab of the customization window lets yous add extensions to Python IDLE. Extensions permit you to add new, awesome features to the editor and the interpreter window. You can download them from the internet and install them to right into Python IDLE.
To view what extensions are installed, select Options → Configure IDLE -> Extensions. At that place are many extensions available on the internet for you lot to read more than about. Find the ones you like and add together them to Python IDLE!
Conclusion
In this tutorial, you've learned all the basics of using IDLE to write Python programs. You know what Python IDLE is and how you can apply it to collaborate with Python directly. You've as well learned how to work with Python files and customize Python IDLE to your liking.
You've learned how to:
- Work with the Python IDLE beat
- Use Python IDLE as a file editor
- Improve your workflow with features to help you lot code faster
- Debug your code and view errors and exceptions
- Customize Python IDLE to your liking
Now you're armed with a new tool that will let yous productively write Pythonic lawmaking and save you endless hours down the road. Happy programming!
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Starting With Python IDLE
coonrodhapteraind.blogspot.com
Source: https://realpython.com/python-idle/
0 Response to "How to Run Function Again From Python Shell"
Post a Comment