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
Activity 1:
Install Python on Windows.
Solution:
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.
Activity 2:
Set the PATH variable to use Python from Windows Command line.
Solution:
- 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.
Activity 3:
Run Python prompt on Windows.
Solution:
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.
Activity 4:
Run Python IDLE on Windows.
Solution:
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.
Activity 5:
Run and use the interpreter prompt.
Solution:
- 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.
Activity 6:
Quit interpreter prompt.
Solution:
- 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]
Activity 7:
Add comment to a python statement.
Solution:
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.
Activity 8:
Run Python scripts from command line. Use any text editor to write the script.
Solution:
- 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”
Activity 9:
Write a Python program that uses multi-line comment.
Solution:
- 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
multicomment.py
#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.
Activity 10:
Use \ to write single Python statement in multiple lines. Try this on Python IDLE.
Solution:
- 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.
Activity 11:
Repeat Activity 10 by writing code in a separate file, using Python IDLE.
Solution:
- 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
Activity 12:
Write multiple statements in a single line.
Solution:
- Run Python IDLE or write python script and run it on command
- Type in the following
print(“hello”); print(“world”)
- Execute the You will see following output:
- In python, semicolon (;) is used to separate two statements, written in a single
Related links
Microsoft word Installation of Python List and Tuple in python
Power Point Variable and statement String in python
Microsoft Excel If else statement in python functions in python
Microsoft Access loops in python Ubunto operating system
At Cui tutorial, courses, past papers and final year projects
#tutorial #cui #pastpaper #courses