This document provides a comprehensive guide on installing Python 3.12.3 on Windows, including steps for installation, testing, and verifying the installation. It also covers the installation of essential packages like numpy, pandas, xlrd, and matplotlib, as well as methods for writing and executing Python programs. Additionally, it explains how to access help and documentation for Python features.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views24 pages
lecture 2 writing first python program
This document provides a comprehensive guide on installing Python 3.12.3 on Windows, including steps for installation, testing, and verifying the installation. It also covers the installation of essential packages like numpy, pandas, xlrd, and matplotlib, as well as methods for writing and executing Python programs. Additionally, it explains how to access help and documentation for Python features.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24
Lecture 2
Writing First Python Program
By Dr. Sunita Varma Installing Python for Windows • Latest version of python is 3.12.3 • Download this version of python by visiting • http://www.python.org/downloads • At the bottom of this page click Windows x86-64 executable installer link • File name by name python-3.12.3-amd64.exe will be downloaded in computer • By double clicking and following instructions python software latest version is installed in computer Installing Python for Windows • Following steps are performed to install python: 1. Double click the python-3.12.3-amd64.exe file 2. Setup dialog box appears 3. Select install launcher for all users and add python 3.12 to PATH checkboxes 4. Click the install now link 5. Setup progress bar will appear 6. When python installation is complete Set was successful message can appear 7. Click close button Testing Installation in Windows12 1. Click Start button on task bar of windows12 operating system • It displays all applications available in computer system in alphabetical order • Go to P and view python 3.12 folder • This folder contains following icons: • IDLE (Python 3.12.3 64-bit) • Python 3.12 (64-bit) • Python 3.12 anuals (64-bit) • Python 3.6 Module Docs (64-bit) Testing Installation in Windows12 2. Click IDLE option • Python’s IDLE: Integrated Development Environment • Graphical user interface window opens • At bottom of screen white icon displayed on taskbar 3. Right click on icon and click pin to taskbar option • Clicking on this taskbar icon will be sufficient to open python IDLE 4. Click python prompt • Tripple greater than sysmbol • Type quit() to close python window • Prompt asking: Do you want to kill it appears • 5. click OK button to quite from IDLE window Testing Installation in Windows12 6. Click windows Start button and then click Python 3.12(64-bit) to open Python command line window which displays black screen • Python command line window is also useful in same manner as python IDLE window to type python code and run it • Right click python command line window icon displayed on taskbar at bottom of monitor and pin it to taskbar so that user can simply click on this icon to open python command line window at any time • Type quit() to quit form command line window Verifying Path to Python • During installation process of Python path is automatically set to Python • Let’s perform following steps to view path to Python directory 1. Right click This PC icon in Windows 12 2. Then click properties option 3. On the displayed page click Advanced System Settings 4. It will display System Properties dialog box 5. Click the Environment Variable button 6. It will display User variables and System variables 7. Select Path variable and click Edit button in User variables 8. Click Edit text button present at right side of Edit Environment Variable dialog box 9. It will display the directories involved in path 10.Observe the directory by name: Python12/Scripts 11. Add dot (.) at end of that path and click ok button 12. Dot represents directory where Python is executed By adding dot python can run in any directory in computer system Installing numpy • Python supports only single dimensional arrays • To create multi-dimensional arrays user need special package called Numerical Python Package or numpy • To download and install this package on system prompt use pip: Python Installation of Packages command • C:\>pip install numpy • Pip program comes with python software by default • This command searches latest version of numpy package on Internet downloads it install it • While using this command computer would be connected to Internet Installing pandas • pandas is package used in data analysis • This package is mostly used by data scientists and data analysts • To download and install package on system prompt give the command: • C:/pip install pandas • command downloads pandas package from Internet and installs Installing xlrd • xlrd is package thst is useful to retrieve data form Microsoft Excel spreadsheet files • It is useful in data analysis • To download and install package give the command on command prompt • C:\>pip install xlrd • Command downloads xlrd packagefron Internet and installs it Installing matplotlib • matplotlib is used to produce good quality 2D graphics • it is used for showing data in form of graph and for designing electronic circuits machinery parts etc • To download and install package on system prompt give command • C:\>pip install matplotlib Verifying Installed Packages • To verify whether installed packages are added in Python software properly or not: • Go into Python and type following command at python prompt • >>> help(‘modules’) • For this purpose first click Python IDLE Window then type preceding command • It will display all module names currently available in user python software Writing First Python Program • Write first python program and execute it • Program for addition of two numbers; • #first program to add two numbers • a = 10 # store 10 into variable a • b = 15 #store 15 into variable b • c = a + b # add 10 and 15 and store into variable c • Print(“Sum=“, c) #display sum Writing First Python Program • Hash symbol # represents comment line in python • Comment is useful to describe elements of program • Comments are not executed by python compiler or PVM • It is not compulsory to write comment but comments improve understanding of program Executing Python Program • There are three ways of executing python program ❑ Using Python’s command line window ❑ Using python’s IDLE graphics window ❑ Directly from system prompt • First two are called interactive modes where user can type program one line at time and PVM executes it immediately • Third one is called non-interactive mode where PVM ask to execute program after typing entire program Using Python’s Command Line Window • Click python’s command line icon • This will open python’s command line window • User can see >>> symbol which is called python prompt • Type first python program at >>> • After typing last line and pressing Enter button can display the result of program • Type exit() or quit at python prompt to terminate PVM and close window Using python’s IDLE Graphics Window • It is possible to execute python program by going to Integrated Development Environment: IDLE • It provides graphical interface to user • Click python’s IDLE icon • This will open python’s IDLE window • Type program after entering last line user can see result • To terminate IDLE window type exit() or quit() at python prompt • It will display message as to kill program or not • Click ok button • Python compiler will terminate and window will close Using python’s IDLE Graphics Window • Running python program in preceding section may not be convenient • When programmer wants to type big program best way is to use File->New option in IDLE window • It opens new window where entire program can be typed • Save the typed program by clicking File->Save • And type file name and .py extension in directory • Program can be run and compile by using options • File Run->Run or pressing F5 button on keyboard • A new window will open where results are displayed • To reopen python program that is already saved in python IDLE window use • File->open option • Go to directory where file is saved and click filename Running Directly from System Prompt • Type the python program in text editor like Notepad or EditPlus • Save program and call python compiler to compile it • Following are steps to run python program directly form system prompt 1. Open text editor and type program 2. Save program by clicking File → Save 3. Program file is stored file name with .py extension 4. While typing python program it is better to put program name inside double quotes 5. Double quotes tell notepad to save program exactly as name is typed and not to save it as text file Running Directly from System Prompt 7. Open command prompt window and go to directory where program is saved 8. This can be done by typing COMMAND or CMD commands at Start→Run 9.In windows 10 operating system first right click on ‘Start’ button and then click ‘Run to see Run dialog box where command is typed 10. Open command prompt 11. Go to directory where python program is saved 12. Program is executed by typing following command on command prompt: c:\user>python file.py Running Directly from System Prompt • Command python is program which contains python compiler as well as PVM • Python compiler compiles and convert program into byte code instructions • Then PVM runs those byte code instructions to produce final results • Python compiler along with PVM is loaded into memory only when python is typed at command prompt window • When program execution is completed and results are displayed python compiler and PVM will be terminated from memory • When another program is executed again python compiler is loaded into memory along with PVM • Following command is typed to exit from command prompt • C:\user>exit Getting Help in Python • When programmer faces some doubt about how to use particular feature of python language he can view help • To get help one can enter help mode by typing help() at python prompt • >>>help() • To see what modules are available in python type • help> topics • To know about any modules type • Help> module name • To quite from help press Enter button without typing anything • To quite form python mode type • >>>quit() or exit() • Another way of getting help is to type • >>>help(topic name in quotes) for example help(‘print’) Getting Python Documentation Help • Python developers have provided extensive description of all python features in document that is called Python Documentation • This provide great help for beginners and for professional programmers to understand all features of python • To see python documentation help open IDLE window select Help → Python Docs Thank you
Python Programming Reference Guide: A Comprehensive Guide for Beginners to Master the Basics of Python Programming Language with Practical Coding & Learning Tips