In our previous blog, we completed installing python and writing our first program. For this blog, we will dive deeper into how programming in python works and various terms you need to know related to python programming: identifier, variable and comment.
Top Features of Python
Easy to Understand
Python has a very simple and easy to understand syntax which makes it easy for Engish speakers to be able to read and write in this language.
Free and Open Source
Python is an open source language, which means that its source code is open to view for anyone (which you can view here). This means that anyone can use python to create anything without paying a single penny. Also, open source ensures that other great developers can contribute to the python repository, making it much better.
Rich Library of Packages
Since python has been a popular language for around 30+ years, hunderds and thounsands of developers together have created amazing packages in python, which allows developers to perform all sorts of actions using it, like creating websites, desktop apps, AI, games etc.
Community Support
Python also has a large community of developers which, over the years, have provided solutions to so many common problems that newbie developers might face from time to time. This makes it so easy for new developers to jump in and learn from others.
Python Execution Modes:
Python has two modes in which we can run our code. The first is the interactive mode and the next is the script mode.
Interactive Mode
The interactive mode allows developers to run python one or few lines of code at a time. To try this, open your computer terminal (or command prompt for Windows) and type
For MacOS/Linux:
python3
For Windows:
python
You should be able to view a display like this:
print("Hello World")
Writing the above line of code and hitting 'Enter', you will be able to view "Hello World" in the console.
Script Mode:
Script mode allows developers to write multiple lines of code in a file and then allow python to execute them all by reading and executing each and every line of code in python one by one. In the previous blog, when we created "main.py" and executed it, we used the script mode in python.
When to use which ?
Interactive mode is generally used for testing out few python lines, while script mode is used in building and running softwares. We will be using the interactive mode for learning python and later script mode when making programs.
Keywords In Python
Keywords in python are words that have a specific meaning or use in all python programs. These keywords are globally present (which means any python script can use them) and are case sensitive (they need to be written exactly the same, no case difference). Here are all the supported keywords in python.
False | None | True | and | as |
assert | async | await | break | class |
continue | def | del | elif | else |
except | finally | for | from | global |
if | import | in | is | lambda |
nonlocal | not | or | pass | raise |
return | try | while | with | yield |
We will learn about the use of each of these keywords as we continue with the approach.
Identifiers
Identifiers in python are like real life names of people. Just like each person has a name which is used to uniquely identify them, python uses identifiers to store any data and uniquely identify it. Just like we follow some general practices for naming people, we need to follow some best practices while creating identifiers in python.
- The name should begin with a letter (a-z, A-Z) or an underscore (_), followed by letters, digits, or underscores.
- It can be of any length. (However, it is preferred to keep it short and meaningful).
- It should not be a keyword or reserved word mentioned above.
- We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
Identifiers like
output, marks1, test123, t12est
are all valid values, however values like
123test, test@1
are invalid. As good coding practices, it is recommended to keep identifiers name in such a way, that they clearly define what data is stored in them. For example:
username
is a better identifiers than
name
to store the name of a user.
Variables in Python
Relation between a variable and an identifier is same as the relation between a person and its name. In programming context, a variable is the data stored in the program and the identifier is the name used to refer to that variable.
A variable in python (and similarly in all programming languages) can store different types of values like a string or a number or a decimal value and many more.
Variables are used in programming to store any value while the program is running. For example, if you let's say try to search for a contact name in your phone, the phone program temporarily stored the name you are trying to search in the phone's memory, and will remove it from its memory once the searching is done. One thing to note here is that variable values get lost once the program has been closed (there are other ways to permanently store data, which we will discuss later).
To create a variable, you simply write a python statement in this format
identifier = value
For example:
name = "John" age = 20
creates two variables with identifier name with value "John" and age with value 20. These values can also be displayed by writing
print(name) print(age)
which displays John and 20 in two lines.
Comments in Python
When we start to write complex code in python, going hundreds and thousands of lines of code, it may become difficult for other developers to understand what the python code is actually doing. To help other developers understand code, while ensuring they don't break something in the code, developers can write comments.
A comment in python is a single or multiple lines of code that are not read or processed by python. Python simply skips these lines when trying to run a program. There are two ways to write comments.
# hashtag to write a single comment ''' or three single quotes to write multiple lines of comments. '''
For example: we can write comments to understand the previous code we just wrote
# storing the name of the person name = "John" # storing the age of the person age = 20 # displaying the name and age of the person print(name) print(age)
It is also considered good coding practices to write proper comments so that other developers don't have issues understanding your code, so from now on make sure to write comments in any python code you write.
Looking Forward
This is it for this blog. As a homework assignment, try to create other details for a person like date of birth, address, hobbies etc. and try to print them on the screen.
For the next blog, we will discuss about data types in python and some operations possible on them.
Till then...keep coding !!!
If you have any queries, please let me know