Introduction To Python
Installation of Python Step by Step
The will teach you to:
- Installation of Python
- Explore Python interpreter and IDLE
- Get familiar with outputting data to the screen
- Writing a basic ‘Hello World’ program
Install Python on Windows.
1. Visit https://www.python.org/downloads/ and download the latest version.
2. Run the downloaded software. Make sure you check option: Add Python 3.9 to PATH.
3. To change install location, click on Customize installation, then Next and enter C:\python3.9 as install location or any other location you want.
4. If not checked, check Add Python to environment variables. This does the same thing as Add Python 3.9 to PATH on the first install screen.
Set the PATH variable to use Python from Windows Command line.
- Click on start.
- Search for python.
- Right click on python 3.9(64 bit).
- Now click on “open file location”. After clicking, below screen will be appear:
- copy the path as shown in below figure:
- Go to start and type “env” and click on “Edit system Environment Variable”.
- Now click on Environment Variables and then click on path.
- Add the copy path and press ok.
Run Python prompt on Windows.
1. For Windows users, you can run the interpreter in the command line if you have set the PATH variable appropriately.
2. To open the terminal in Windows, click the start button and click Run. In the dialog box, type cmd and press [enter] key.
3. Then, type python or py and ensure there are no errors.
Run Python IDLE on Windows.
IDLE (Integrated Development Environment or Integrated Development and Learning Environment) is an integrated development environment for Python, which has been bundled with the default implementation of the language.
IDLE is intended to be a simple IDE and suitable for beginners, especially in an educational environment. According to the included README, its main features are:
Multi-window text editor with syntax highlighting, autocompletion, smart indent and other.
Python shell with syntax highlighting.
Integrated debugger with stepping, persistent breakpoints, and call stack visibility.
1. Click on Start.
2. Search for Python IDLE.
3. Select it.
Run and use the interpreter prompt.
- Open the terminal in your operating system.
- Open the Python prompt by typing py and pressing [enter]
- Once you have started Python, you should see >>> where you can start typing This is called the Python interpreter prompt.
- At the Python interpreter prompt, type :print(“Hello World”)followed by the [enter] You should see the words Hello World printed to the screen.
- Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement. We use print to print any value that you supply to it. Here, we are supplying the text Hello World and this is promptly printed to the screen.
Quit interpreter prompt.
- If you are using a GNU/Linux or OS X shell, you can exit the interpreter prompt by pressing [ctrl + d] or entering exit() (note: remember to include the parentheses, () followed by the [enter]
- If you are using the Windows command prompt, press [ctrl + z] followed by the [enter]
Add comment to a python statement.
Comments are any text to the right of the #symbol and are mainly useful as notes for the reader of the program.
- Open Python
- Type print(‘hello world’) # Note that print is a function. Press Enter.
- Comments are not executed i.e. output of the statement would still be hello world.
Run Python scripts from command line. Use any text editor to write the script.
- Type the text below into a text editor and save as py. Python files usually have the .py extension.
- If you are using Windows and Notepad++, it may look something like this:
- To start the program, open the command line and type py hello.py
- You should see a line of text showing “hello world”
Write a Python program that uses multi-line comment.
- Single line comments start with # while multi-line comments use three single- quotes before and after the part you want to be commented.
- Type the following piece of code in text editor and save it with the name
#print(“I am a single line comment”) print(“I am not a comment”)
print(“we are in a multiline comment) print(“we are still in a comment”)
print(“we are out of the comment”)
- Execute this script on command The result will not show single and multi- line commented statements/lines.
Use \ to write single Python statement in multiple lines. Try this on Python IDLE.
- Run Python IDLE and type in print(“hello world”). Press Enter. The output
hello world will be displayed on screen.
- print(“hello world”) is a single statement of Python. To write single statement in multiple lines (for example, if a single line gets too long and it should be written in multiple lines for ease of understanding), backslash \ is
- Type print(“hello \ and press Enter. Now the cursor is in next Type
world”) and press Enter.
Repeat Activity 10 by writing code in a separate file, using Python IDLE.
- Run Python
- Go to File->New File. A new window will be
- Type in the code as shown
- Go to File->Save As and save the file with the name py
- Go to Run->Run Module. The output will be displayed on the Python
Write multiple statements in a single line.
- Run Python IDLE or write python script and run it on command
- Type in the following
- Execute the You will see following output:
- In python, semicolon (;) is used to separate two statements, written in a single
At Cui tutorial, courses, past papers and final year projects
#tutorial #cui #pastpaper #courses