Python:
Learn Python in One Week
by
Kilber Stewart
Copyrigh t 2020 by Kilber Stewart All rights reserved.
This book is copyright, and no part may be reproduced or transmitted through any means, be it mechanical, electronic, internet, or otherwise without the permission of the author except in brief quotation or review of the book.
Table of Contents
Introduction
Computers do not understand human language, so we write a program in a language which a computer can understand. There are many different programming languages like BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc. In this book, you will learn about Python.
Guido Van Rossum created Python while he was working at CWI (Centrum Wiskunde & Informatica), which is based in the Netherlands. Python was released in 1991. Python got its name from a BBC comedy series from the seventies- Monty Pythons Flying Circus.
Some of the features which make Python different from other programming languages are as follows:
- Easy to code and easy to read
Python is very easy to code as compared to other popular languages like Java and C++. It is straightforward for anyone to learn Python syntax in just a few hours. Also, Python is easy to read as it is similar to the English language.
- Free and Open-Source
Python is freely available. You can download it from https://www.python.org/downloads/
Also, it is open-source. It means its source code is open to the public. One can download it, change it, use it, and distribute it without any restrictions.
- High-Level
It is a high-level language, meaning one does not need to remember the system architecture. Also, as a programmer, we do not need to manage memory. These features make Python more programmer-friendly.
- Portable
Suppose you write a Python code for Windows and want to run on Mac. Then you do not need to make changes to it. In simple words, you can run Python code on any machine. You dont need to write different codes for different machines. This feature makes Python a portable language.
- Interpreted
If you have written code in C++ or Java, you must first compile it and then run it. But in the case of Python, you dont need to compile it. By interpreted, we mean the source code is executed line by line, and not all at once.
- Object-Oriented
An object-oriented language is a language that can model the real world. On the other hand, a procedure-oriented language focuses on functions, which are code that can be reused. Python supports both object-oriented and procedure-oriented programming.
- Large Standard Library
Python has an extensive library, meaning you dont have to write code for every single thing. The library comes with pre-written codes which you can use while writing codes. There are libraries for regular expressions, documentation-generation, unit-testing, web browsers, threading, databases, CGI, image manipulation, and many more.
What is a Program?
A program is a set of instructions that is processed by a computer to perform a specific task. The task can be mathematical or working with text.
To write and run a Python program, an interpreter is needed. IDLE (Integrated Development Environment) is the most popular development environment for Python. In IDLE, you can edit, run, browse and debug your Python code. In this environment, it is effortless to write programs.
In this book, we will be using 3.8.5 IDLE to write and run code. You can download it from https://www.python.org/downloads/ .
Simply click on Download Python 3.8.5 and the software will be installed on your computer.
How to use Python Shell, IDLE, and Writing FIRST program
IDLE is a program that comes with a Python interpreter.
To launch the IDLE program, go to the start menu and in the search box type IDLE. Once you find IDLE, click on it, and a window will be presented to you, as shown below:
This is an interactive mode to use Python, meaning you can enter one command at a time, and the Shell will return the result of the typed command once the command is executed the Shell waits for the next command.
You will type command at the lines starting with >>>. For example, type the following commands:
>>> 5+2
>>> 5>7
False
>>> print ('Hello World')
Hello World
When you type 5+2, you are giving a command to the Shell, asking it to perform the mathematical operation of addition. That is why the Shell returns the value 7. When you type 5>7, you are asking the Shell if 5 is greater than 7, so the Shell replies False. In the next line, the prin t command asks the Shell to display the string Hello World.
If you exit from the Python Shell and enter it again, all the commands you typed will be gone. In other words, you cannot create an actual program with Python Shell. Python shell is suitable for testing specific commands once you are getting started with Python. To code an actual program, you will have to write your code in a text file and save it with a .py extension. This type of file is also known as a Python script.
To create a Python script, simply click on File > New File on the menu bar at the top of your Python Shell. This will open a new window in the form of text editor where you will write your program.
Let us write our first program called the Hello World program.
Type the following code into your text editor.
#This code prints the words "Hello World"
print("Hello World")
As shown in the figure below, if you closely notice, line #This code prints the words Hello World is in red. On the other hand, the word prin t is in purple, and Hello World is in green. The words prin t and Hello World serve different purposes in our program, so they are displayed in different colors.
In the above code #This code prints the words Hello World which i s displayed in red is not part of the program. This is a comment written to make our code more readable for other programmers.
The Python interpreter ignores this line of code. To add comments to your program, simply type a # sign before writing a comment as typed below:
#This is a comment
#This is also a comment
#This is yet another comment
Also, we can use three single quotes for writing multiline comments, for example:
This is a comment
This is also a comment
This is yet another comment
To save your code, simply click on File > Save As keep in mind to save your code with .py extension.
Finally, to execute the program, simply click on Run > Run Module or press F5. Now you will see the words Hello Worl d printed on your Python Shell.
Chapter 3- Variables and Operators
In this chapter, you will learn about variables and operators. You will learn about what variables are and how to declare them and name them. You will also learn about the common operations that you can perform on variables.