Close
Close

Python Basics


Now that you know how to print any value on the screen, let’s look into some basics and terminologies you should know while learning Python. This is just an introductory chapter to make you familiar with the common terminologies and basic concepts.

Python Statements


A program is nothing but just a set of instructions that are executed by the computer to perform some task. In Python, the instructions that can be executed by a Python interpreter are called statements.

Look at the following code.

num = 10
print('num =', num)

This code has two statements. The first statement num = 10 assigns a value of 10 to a variable num, and the second statement print('num =', num) prints num = 10.

statements in Python

Python Multiline Statements


Sometimes a statement can get lengthy and we might want to break it into multiple lines just to make it more readable. We can do so by using the line continuation character (\).

Consider the following statement.

num = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

This statement can be written over multiple lines as follows.

num = 1 + 2 + 3 + \
	4 + 5 + 6 + \
	7 + 8 + 9 + 10

Look at another example in which a long text is divided into multiple substrings that are written over different lines.

message = "Hello learners, Python is a wonderful language to code in. " \
        "This text is written over multiple lines to improve readability " \
        "but it will be printed in a single line on screen."

print(message)
Output
Hello learners, Python is a wonderful language to code in. This text is written over multiple lines to improve readability but it will be printed in a single line on screen.

Multiline statements in Python

Parentheses ( ), brackets [ ], and braces { } automatically applies line continuation to anything written inside them and we don’t need to explicitly use line continuation character (\). The multiline statements in the above two examples can be rewritten using parentheses as shown below.

num = (1 + 2 + 3 +
	4 + 5 + 6 +
	7 + 8 + 9 + 10)
message = ("Hello learners, Python is a wonderful language to code in. "
        "This text is written over multiple lines to improve readability "
        "but it will be printed in a single line on screen.")

Python Multiple Statements in a Single Line


In Python, we can write multiple statements in a single line by separating them by semicolons (;).

a = 10; print(a); print('Hello World')
Output
10
Hello World
But this is generally not practised by Python programmers and you should avoid it too.

Python Comments


Do you know, you can include some comments in your code which won't be compiled and your computer will simply ignore them while running your code. They are written to make our code more readable. Let’s look at them for better understanding.

Python Single Line Comments


Single line comments always start with '#'. The next line is not a part of the comment.

# I am using a variable x
x = 10
print(x)
Output
10

Python Multiline Comments


You can also write comments which extend upto multiple lines. Multiline comments are written within 3 single quotes ''' ''' or 3 double quotes """ """.

'''
Multiline comment
Using variable x and y.
'''
x = 10
y = 5
print(x+y)
Output
15

Comments in Python

Why to use Comments?


As mentioned earlier, it makes your code more understandable. Assume that you have written a software and after releasing it, you hired a few good programmers for its maintenance. Without comments, it would be a very difficult job for them to understand your code. And most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make a habit of writing good comments.

It is also not recommended to write too many comments. A clean and good code can be understood without comments.

Python Keywords


There are few words that are used by the Python language itself. So, we can't use those words for the name of our variables as they are reserved for Python. For example, take the function which we have been using till now - print. The name ‘print’ is used by Python itself and thus we can’t use it as our variable name. Doing so will give us an error.

Here is the list of a few keywords reserved for Python.

and assert as
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 True
False None

Python Identifiers


An identifier is a name we give to entities like variables, functions, classes, etc. We will learn about these entities in later chapters. For now, just remember that whenever we define an entity, we assign it a name which is called an identifier. For example,

x = 10

Here, x is an identifier.

Rules for writing Python Identifiers


  1. Identifiers can be a combination of lowercase (a - z) and uppercase (A - Z) letters, digits (0 - 9) or an underscore (_). Special characters (*, %, #, !, @, $, etc.) cannot be present in identifiers. Eg, Message1, num_max are valid identifiers and num@ is an invalid identifier.
  2. Identifiers cannot start with a digit. Eg, 2num is an invalid identifier.
  3. In Python, identifier names are case-sensitive, i.e. num and Num will be treated as different identifiers.
  4. Keywords cannot be used as identifiers. Eg, break is an invalid identifier.

Good Practices to Name Python Identifiers


There are some good practices to name identifiers which should be followed but are not mandatory.

  1. Identifiers should be kept meaningful so that understanding code is easier. Therefore, instead of using names like ,x, y, a, b, etc for identifiers, use some meaningful names. For example, if we are storing the product of two numbers in a variable, then the name of that variable should be kept as product.
  2. Names of variables and functions should contain only lowercase letters. For example, name, display().
  3. If you want to name a variable or function with multiple words, then name it as multiple words separated by underscores. For example, first_name and last_name.
  4. Class names should start with capital letters. For example, Person, Student, Books, etc.
  5. If you want to name a class with multiple words, then name it in CamelCase format. For example, StudentRecord, BookAuthor, etc.
  6. Avoid putting underscore (_) or double underscore (__) at the start and end of identifier names, because these have some special meaning in Python.

Wooh! This chapter was a lengthy one but it was just to introduce you to the basics of Python. We will look at variables in somewhat more detail in the next chapter.

There is no glory in practice but there is no glory without practice.


Download Our App.
BlogsDope App
Get it on Google Play
Ask Yours
Post Yours
Doubt? Ask question