[Cheat Sheet] Basic Python 3 Syntax Explained with Code and Examples

[Cheat Sheet] Basic Python 3 Syntax Explained with Code and Examples

Basic Python 3 Syntax

Each syntax is explained with a Python programming example.

Let’s dive in…

1. Print Statement

print('Hello, world!') #Hello, world!

Printing variable data:

strName = "Chris"
print('My name is', strName) #My name is Chris

2. Comment

The comment is the part of the code that does not get executed.

In programming, it is mainly used for describing the logic of the code and for other purposes.

One-line comment:

#this is my first comment

Multi-line comment:

I am new to Python language.
It is an amazing programming language.
You should start learning.

3. Variable Declaration

Python has a dynamic data type. You don’t need to specify the data type of the variable while declaring. The data type of the variable is decided based on the type of data passed to the variable.


var = 10 #integer variable

var = "Why does everyone love Python?" #string variable

4. String Variable

In programming, the string is the set of characters.

Declaring string variable:

myStr = 'My string'


myStr = "This is the string"

Declaring Multi-line string variable:

myStr = ''' String is very useful data structure.
There are many functions available in Python
to manipulate the string variable.'''

5. Arithmetic Operations

varA = 20
varB = 6
print(varA+varB) #26
print(varA-varB) #14
print(varA*varB) #120
print(varA/varB) #3.3333333333333335
print(varA%varB) #2

Here, division operation is interesting. In Python 3, 20/6 gives 3.3333333333333335. Whereas, in Python 2, 20/6 gives 3. All other arithmetic operations give the same result in Python 2 and Python 3 versions. Try executing code online to know things practically.

Apart from integer, there are various other numeric data types in Python.

6. if-else Statement

if statement:

var = 10
if var > 0:
    print("It is a positive number.")

if-else statement:

var = -10
if var > 0:
    print("It is a positive number.")
    print("It is not a positive number.")

7. for loop

Loop is used to execute the same lines of code multiple times.

Example: Print the number from 1 to 9.

for i in range(1,10):

Read more about the range() method.

8. Definition/Function

There are two parts. First, you have to declare the function. Then call the function to execute the code written inside the function.

def myFirstFunc():
   print("Thanks for calling!")

myFirstFunction() #calling function

9. Taking User Input from Keyboard

You can also take the user input from the keyboard.

strName = input("Enter your good name:")

10. List in Python

The list is somewhat similar to the array in C/C++ programming. It stores multiple values in one variable.

Example: Create an employee list that stores the employee-id, name, and salary.

empList = [45, 'Alice', 2560]
print(empList[0]) #45
print(empList[1]) #Alice
print(empList[2]) #2560
#looping over all the elements in the list.
for i in empList:

11. Tuple in Python

The tuple is a little bit different from the list in Python.

Example: Create a Python tuple to store the employee data like employee id, name, salary.

empTup = (45, 'Alice', 2560)
print(empTup[0]) #45
print(empTup[1]) #Alice
print(empTup[2]) #2560

Read the difference between list and tuple in Python.

12. Dictionary in Python

The Dictionary variable stores the key-value pair.

Example: Create a dictionary that stores the employee id and his/her name.

empDict = {12: 'Mark', 45: 'Bob'}
#accesing dictionary value from its key
print(empData[12]) #Mark
print(empData[45]) #Bob

You can perform various operations on the dictionary. Read detail in the Python dictionary tutorial.

13. __name__ and __main__ in Python

When you run your Python program, the interpreter does the following things.

  • It sets some important variable, one of that is __name__.
  • Set the value of __name__ variable as "__main__"
  • And then start executing the code in the file.

You might have seen this variable in most of the Python program files. Aren’t you?


def myFirstFunction():
    print("Thanks for calling.")

def mySecondFunction():
    print("I like Python Programming.")

if __name__ == "__main__":

What is the use and purpose of it in Python 3?

Every time we run this Python program, the “if” condition will be true and the code inside this block will be executed.

We can set the flow of our program by checking the value of a __name__ variable.

14. File Handling (Read/Write Operations)

I have explained all the syntaxes in another tutorial on file handling in Python.

It covers all the file-related operations in Python like – How to create and open a text file? How to read and write a text file?

Go through it.

15. Bitwise Operators in Python

There are basically 6 bitwise operators defined in Python – AND, OR, NOT, XOR, RIGHT SHIFT, and LEFT SHIFT.

Check Python bitwise operators where I have explained each operator with examples.


This is all about basic Python 3 syntax with the code.

If you are preparing for Job or any competitive programming contest, bookmark this page. Whenever required you can easily go through all these syntaxes.

You can never learn programming simply by going through these syntaxes. Try to use these concepts and code in your Python programming. Practicing is the only way of harnessing any computer programming language.

If you have any doubt or if you have any questions, ask me in the comment.

Happy Pythoning!


  1. Your blog is excellent, I request you to revisit and update the following sections

    6. if-else Statement

    Replace printf with print in both the statements

    7. for loop

    Replace colon with a comma in range method.

  2. Your blog is excellent and I learn new things from this blog, I request you to revisit and update the following sections
    11. Tuple in python
    Replace print(empTup(0)) with print(empTup[0]) in all statement of tuple.

  3. The arithmetic operator comments are incorrect.
    The integers when divided by print 3.3333333333333335
    is printed.

  4. In both 10. List in Python and 11. Tuple in Python, empList([2]) is initialized as 2560 but the print of empList([2]) is transposed to 2506

    EmpList = [45, 'Alice', 2560]
    print(empList[2]) #2506
  5. I get the following on the division in Python 2:
    Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
    [GCC 7.3.0] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.

    >>> print(20/6)
    >>> six=6
    >>> twenty=20
    >>> print(twenty/six)

Leave a Reply

Your email address will not be published. Required fields are marked *