Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/vol9_6/ on line 757

Notice: Trying to access array offset on value of type bool in /home/vol9_6/ on line 2360

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/vol9_6/ on line 986
Simple Way To Learn Python – Tech Savvy

Simple Way To Learn Python

Tech Savvy Experts here to help you....

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.

  1. Download IDLE.
  2. Run the installer to install IDLE on your computer.
  3. Go to File > New. Then save the file with .py extension. For example, etc.
    You can give any name to the file. However, the file name should end with .py
  4. Write Python code in the file and save it.
  5. 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.

  1. Download the latest version of Python.
  2. 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 are able to 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.

Run Python in Immediate mode

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.

Python IDLE

Now you can create a new file and save it with .py extension. For example,

Write Python code in the file, save it. To run the file go to Run > Run Module or simply click F5.

Run Python program in IDLE

Your first Python Program

Now that we have Python up and running, we can write our first Python program.

Let’s create a very simple program called "Hello World!".  A “Hello, World!” is a simple program that outputs Hello, World! on the screen. Since it’s a very simple program, it’s often used to introduce a new programming language to a newbie.

Type the following code in any text editor or an IDE and save it as

print("Hello world!")

Then, run the file. You will get the following output.

Hello world!

Congratulations! You just wrote your first program in Python.

As we can see, it was pretty easy. This is the beauty of Python programming language.

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 in the course of time.

All the keywords except TrueFalse and None are in lowercase and they must be written as it is. The list of all the keywords is given below.


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

  1. 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 like myClassvar_1 and print_this_to_screen, all are valid example.
  2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
  3. Keywords cannot be used as identifiers.
    1. >>> global = 1
    2. File "<interactive input>", line 1
    3. global = 1
    4. ^
    5. SyntaxError: invalid syntax
  4. We cannot use special symbols like !@#$% etc. in our identifier.
    1. >>> a@ = 0
    2. File "<interactive input>", line 1
    3. a@ = 0
    4. ^
    5. SyntaxError: invalid syntax
  5. 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 name identifiers that make sense.

While, c = 10 is valid. Writing count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long gap.

Multiple words can be separated using an underscore, this_is_a_long_variable.

Python Statement

Instructions that a Python interpreter can execute are called statements. For example, a = 1is an assignment statement. if statement, for statement, while statement etc. are other kinds of statements which will be discussed later.

Multi-line statement

In Python, end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\). For example:

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

This is explicit line continuation. In Python, line continuation is implied inside parentheses ( ), brackets [ ] and braces { }. For instance, we can implement the above multi-line statement as

a = (1 + 2 + 3 +    4 + 5 + 6 +    7 + 8 + 9)

Here, the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [ ] and { }. For example:

colors = ['red',          'blue',          'green']

We could also put multiple statements in a single line using semicolons, as follows

a = 1; b = 2; c = 3

Python Indentation

Most of the programming languages like C, C++, Java use braces { } to define a block of code. Python uses indentation.

A code block (body of a function, loop etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be consistent throughout that block.

Generally four whitespaces are used for indentation and is preferred over tabs. Here is an example.

for i in range(1,11):
if i == 5:



1234for i in range(1,11): print(i) if i == 5: breakRun

The enforcement of indentation in Python makes the code look neat and clean. This results into Python programs that look similar and consistent.

Indentation can be ignored in line continuation. But it’s a good idea to always indent. It makes the code more readable. For example:

if True:    print('Hello')    a = 5


if True: print('Hello'); a = 5

both are valid and do the same thing. But the former style is clearer.

Incorrect indentation will result into IndentationError.

Python Comments

Comments are very important while writing a program. It describes what’s going on inside a program so that a person looking at the source code does not have a hard time figuring it out. You might forget the key details of the program you just wrote in a month’s time. So taking time to explain these concepts in form of comments is always fruitful.

In Python, we use the hash (#) symbol to start writing a comment.

It extends up to the newline character. Comments are for programmers for better understanding of a program. Python Interpreter ignores comment. 

#This is a comment#print out Helloprint('Hello')

Multi-line comments

If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning of each line. For example:

#This is a long comment#and it extends#to multiple lines

Another way of doing this is to use triple quotes, either ''' or """.

These triple quotes are generally used for multi-line strings. But they can be used as multi-line comment as well. Unless they are not docstrings, they do not generate any extra code.

"""This is also aperfect example ofmulti-line comments"""

Docstring in Python

Docstring is short for documentation string.

It is a string that occurs as the first statement in a module, function, class, or method definition. We must write what a function/class does in the docstring.

Triple quotes are used while writing docstrings. For example:

def double(num):

    """Function to double the value"""

    return 2*num

123def double(num): “””Function to double the value””” return 2*num

Docstring is available to us as the attribute __doc__ of the function. Issue the following code in shell once you run the above program.

>>> print(double.__doc__)Function to double the value

Python Variables

A variable is a named location used to store data in the memory. It is helpful to think of variables as a container that holds data which can be changed later throughout programming. For example,

number = 10

Here, we have created a named number. We have assigned value 10 to the variable.

You can think variable as a bag to store books in it and those books can be replaced at any time.

number = 10number = 1.1

Initially, the value of number was 10. Later it’s changed to 1.1.

Note: In Python, we don’t assign values to the variables, whereas Python gives the reference of the object (value) to the variable.

Assigning a value to a Variable in Python

As you can see from the above example, you can use the assignment operator = to assign a value to a variable.

Example 1: Declaring and assigning a value to a variable

website = ""

When you run the program, the output will be:

In the above program, we assigned a value to the variable website. Then we print the value assigned to website i.e.

Note : Python is a type inferred language; it can automatically know is a string and declare website as a string.

Example 2: Changing the value of a variable

website = ""
 assigning a new variable to website
 website = ""


When you run the program, the output will be:

In the above program, we have assigned to the website variable initially. Then, it’s value is changed to

Example 3: Assigning multiple values to multiple variables

a, b, c = 5, 3.2, "Hello"
 print (a)
 print (b)
 print (c)


The second program assigns the same string to all the three variables x, y and z.


A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be changed later.

Non technically, you can think of constant as a bag to store some books and those books cannot be replaced once placed inside the bag.

Assigning value to a constant in Python

In Python, constants are usually declared and assigned on a module. Here, the module means a new file containing variables, functions etc which is imported to main file. Inside the module, constants are written in all capital letters and underscores separating the words.

Example 3: Declaring and assigning value to a constant

Create a

PI = 3.14GRAVITY = 9.8

Create a

import constantprint(constant.PI)print(constant.GRAVITY)

When you run the program, the output will be:


In the above program, we create a module file. Then, we assign the constant value to PI and GRAVITY. After that, we create a file and import the constant module. Finally, we print the constant value.

Note: In reality, we don’t use constants in Python. The globals or constants module is used throughout the Python programs.

Rules and Naming convention for variables and constants

  1. Create a name that makes sense. Suppose, vowel makes more sense than v.
  2. Use camelCase notation to declare a variable. It starts with lowercase letter. For example:myName myAge myAddress
  3. Use capital letters where possible to declare a constant. For example:PI G MASS TEMP
  4. Never use special symbols like !, @, #, $, %, etc.
  5. Don’t start name with a digit.
  6. Constants are put into Python modules and meant not be changed.
  7. Constant and variable names should have combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example:snake_case MACRO_CASE camelCase CapWords


Literal is a raw data given in a variable or constant. In Python, there are various types of literals they are as follows:

Numeric Literals

Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float and Complex.

Example 4: How to use Numeric literals in Python?

a = 0b1010 #Binary Literals
 b = 100 #Decimal Literal 
 c = 0o310 #Octal Literal
 d = 0x12c #Hexadecimal Literal
 Float Literal
 float_1 = 10.5 
 float_2 = 1.5e2
 Complex Literal
 x = 3.14j
 print(a, b, c, d)
 print(float_1, float_2)
 print(x, x.imag, x.real)

10 100 200 300
 10.5 150.0
 3.14j 3.14 0.0

When you run the program, the output will be:

10 100 200 300
10.5 150.0
3.14j 3.14 0.0

In the above program,

  • We assigned integer literals into different variables. Here, a is binary literal, b is a decimal literal, c is an octal literal and d is a hexadecimal literal.
  • When we print the variables, all the literals are converted into decimal values.
  • 10.5 and 1.5e2 are floating point literals. 1.5e2 is expressed with exponential and is equivalent to 1.5 * 102.
  • We assigned a complex literal i.e 3.14j in variable x. Then we use imaginary literal (x.imag) and real literal (x.real) to create imaginary and real part of complex number.

To learn more about Numeric Literals, refer Python Numbers.

String literals

A string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes.

Example 7: How to use string literals in Python?

strings = "This is Python"
 char = "C"
 multiline_str = """This is a multiline string with more than one line code."""
 unicode = u"\u00dcnic\u00f6de"
 raw_str = r"raw \n string"


This is Python
 This is a multiline string with more than one line code.
 raw \n string

When you run the program, the output will be:

This is Python
This is a multiline string with more than one line code.
raw \n string

In the above program, This is Python is a string literal and C is a character literal. The value with triple-quote """ assigned in the multiline_str is multi-line string literal. The u"\u00dcnic\u00f6de" is a unicode literal which supports characters other than English and r"raw \n string" is a raw string literal.

Boolean literals

A Boolean literal can have any of the two values: True or False.

Example 8: How to use boolean literals in Python?

x = (1 == True)
 y = (1 == False)
 a = True + 4
 b = False + 10
 print("x is", x)
 print("y is", y)
 print("a:", a)
 print("b:", b)


x is True
 y is False
 a: 5
 b: 10

When you run the program, the output will be:

x is True
y is False
a: 5
b: 10

In the above program, we use boolean literal True and False. In Python, True represents the value as 1 and False as 0. The value of x is True because 1 is equal to True. And, the value of y is False because 1 is not equal to False.

Similarly, we can use the True and False in numeric expressions as the value. The value of a is 5 because we add True which has value of 1 with 4. Similarly, b is 10 because we add the False having value of 0 with 10.

Special literals

Python contains one special literal i.e. None. We use it to specify to that field that is not created.

Example 9: How to use special literals in Python?

drink = "Available"
 food = None
 def menu(x):
     if x == drink:



When you run the program, the output will be:


In the above program, we define a menu function. Inside menu, when we set parameter as drink then, it displays Available. And, when the parameter is food, it displays None.

Literal Collections

There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.

Example 10: How to use literals collections in Python?

fruits = ["apple", "mango", "orange"] #list
 numbers = (1, 2, 3) #tuple
 alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
 vowels = {'a', 'e', 'i' , 'o', 'u'} #set

['apple', 'mango', 'orange']
 (1, 2, 3)
 {'c': 'cat', 'a': 'apple', 'b': 'ball'}
 {'u', 'i', 'o', 'a', 'e'}