Table of Contents
Each syntax is explained with a Python programming example.
Let’s dive in…
print('Hello, world!') #Hello, world!
Printing variable data:
strName = "Chris" print('My name is', strName) #My name is Chris
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. '''
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.
Example:
var = 10 #integer variable var = "Why does everyone love Python?" #string variable
In programming, the string is the set of characters.
Declaring string variable:
myStr = 'My string'
Or
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.'''
varA = 20 varB = 6 #addition print(varA+varB) #26 #subtraction print(varA-varB) #14 #multiplication print(varA*varB) #120 #division print(varA/varB) #3.3333333333333335 #modular 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.
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.") else: print("It is not a positive number.")
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): print(i)
Read more about the range() method.
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
You can also take the user input from the keyboard.
strName = input("Enter your good name:") print(strName)
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: print(i)
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.
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.
When you run your Python program, the interpreter does the following things.
__name__
.__name__
variable as "__main__"
You might have seen this variable in most of the Python program files. Aren’t you?
Example:
def myFirstFunction(): print("Thanks for calling.") def mySecondFunction(): print("I like Python Programming.") if __name__ == "__main__": myFirstFunction() mySecondFunction()
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.
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.
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!
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.
Thanks for the correction, Sumanth!
Edited. Please have a look.
I am so glad you like my blog. Please give me a minute and submit your feedback (https://www.csestack.org/feedback/) about my Python tutorial.
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))
withprint(empTup[0])
in all statement of tuple.We fixed it.
Thanks, Janki for your kind words!
Take Care!
The arithmetic operator comments are incorrect.
The integers when divided by print 3.3333333333333335
is printed.
Thanks for the correction.
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
It’s by mistake. Thanks for letting us know. Fixed it.
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.
This is expected in Python 2.
If you run the same code in Python 3, you will get the output as 3.3333333333333335.