credit to: https://www.programiz.com/python-programming/first-program
Day 1. About Python
Python is a cross-platform programming language, which means that it can run on multiple platforms like Windows, macOS, Linux, and has even been ported to the Java and .NET virtual machines. It is free and open-source.
How to get started with Python.
The Easiest Way to Run Python
The easiest way to run Python is by using Thonny IDE.
The Thonny IDE comes with the latest version of Python bundled in it. So you don't have to install Python separately.
Follow the following steps to run Python on your computer.
- Download Thonny IDE.
- Run the installer to install Thonny on your computer.
- Go to: File > New. Then save the file with
.py
extension. For example,hello.py
,example.py
, etc.
You can give any name to the file. However, the file name should end with .py - Write Python code in the file and save it.
- Then Go to Run > Run current script or simply click F5 to run it.
Install Python Separately
If you don't want to use Thonny, here's how you can install and run Python on your computer.
- Download the latest version of Python.
- Run the installer file and follow the steps to install Python
During the install process, check Add Python to environment variables. This will add Python to environment variables, and you can run Python from any part of the computer.
Also, you can choose the path where Python is installed.
Once you finish the installation process, you can run Python.
1. Run Python in Immediate mode
Once Python is installed, typing python
in the command line will invoke the interpreter in immediate mode. We can directly type in Python code, and press Enter to get the output.
Try typing in 1 + 1
and press enter. We get 2
as the output. This prompt can be used as a calculator. To exit this mode, type quit()
and press enter.
2. Run Python in the Integrated Development Environment (IDE)
We can use any text editing software to write a Python script file.
We just need to save it with the .py
extension. But using an IDE can make our life a lot easier. IDE is a piece of software that provides useful features like code hinting, syntax highlighting and checking, file explorers, etc. to the programmer for application development.
By the way, when you install Python, an IDE named IDLE is also installed. You can use it to run Python on your computer. It's a decent IDE for beginners.
When you open IDLE, an interactive Python Shell is opened.
Now you can create a new file and save it with .py extension. For example, hello.py.
Write Python code in the file and save it. To run the file, go to Run > Run Module or simply click F5..
Now that we have Python up and running, we can write our first Python program.
Congratulations! You just wrote your first program in Python
Day 2. Python Keywords and Identifiers
In this tutorial, you will learn about keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.).
Python Keywords
Keywords are the reserved words in Python.
We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.
In Python, keywords are case sensitive.
There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.
All the keywords except True
, False
and None
are in lowercase and they must be written as they are. The list of all the keywords is given below.
False | await | else | import | pass |
None | break | except | in | raise |
True | class | finally | is | return |
and | continue | for | lambda | try |
as | def | from | nonlocal | while |
assert | del | global | not | with |
async | elif | if | or | yield |
Looking at all the keywords at once and trying to figure out what they mean might be overwhelming.
If you want to have an overview, here is the complete list of all the keywords with examples.
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.
Rules for writing identifiers
- Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore
_
. Names likemyClass
,var_1
andprint_this_to_screen
, all are valid example. - An identifier cannot start with a digit.
1variable
is invalid, butvariable1
is a valid name. - Keywords cannot be used as identifiers.
Outputglobal = 1
File "<interactive input>", line 1 global = 1 ^ SyntaxError: invalid syntax
- We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a@ = 0
OutputFile "<interactive input>", line 1 a@ = 0 ^ SyntaxError: invalid syntax
- An identifier can be of any length.
Things to Remember
Python is a case-sensitive language. This means, Variable
and variable
are not the same.
Always give the identifiers a name that makes sense. While c = 10
is a valid name, writing count = 10
would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap.
Multiple words can be separated using an underscore, like this_is_a_long_variable
.