Introduction to Python 3 Commands
Python 3 commands have been introduced with Python 2 features and it is compatible with python 2 with help of using some inbuilt keyword to support the functionalities. Python 3 command was released in the year 2008. Python 3 is more intuitive to programmers and more precise while providing the result. Syntax has been changed for python 3 to get more clarity on what is written and what should be the output.
Basic Python 3 Commands
Basic Commands of Python 3 are as follows:
1. Print function
In Python 3, the print function is used with parenthesis to show the output which is not available in python 2.
Syntax:
print (“Content to display”)
Example:
print (“Hello World”)
2. Input function
In this python 3 commands, the entered data is always treated as string even if the data is inserted without ‘ ‘.
Syntax/ example:
>>>X: input(“x:”)
X: 5
>>>X
‘5’
3. Integer division
In Python 3, the division is more precise than python 2 and gives a better result.
Syntax/ example:
>>>X:3/2
1.5 #by default it will give this number which is not available for Python 2
4. range function
In Python 3, the Xrange function of python 2 has been renamed as range function in python 3
4.8 (7,888 ratings)
View Course
Syntax/ example:
range ()
5. raise function
In Python 3, an exception argument has to be put in parenthesis.
Syntax/ example:
raise an exception (“There is some issue”)
6. Arguments
In python 3 commands, arguments need to be declared with the help of keyword ‘as’
Syntax/ example:
except issue an error
7. Next function
In python 3, next function is being used and .net function cannot be used as it throws an error named as attribute error.
Syntax/ example:
next (abc)
8. Unicode
In python 3, strings are mainly stored as Unicode that is utf-8 strings and having byte classes as bytes and byte array.
9. Decision statement
If else statement is being used for decision making in python 3.
Syntax/ example:
var=10
If (var==10) : print (“Value is 10”)
Print (“Ok”)
10. Strings
In python 3, square brackets along with index is using to get the substring.
Syntax/ example:
var = “Hello World”
Print (“var[0]: ”, var [0]) #result would be ‘H’
Intermediate Python 3 Commands
Intermediate Commands of Python 3 are as follows:
1. For loop
In Python 3, for loop is being used to execute the statement multiple times.
Syntax/ example:
List= [1,2,3]
For x in list:
Print (x, end = “”)
2. While loop
it will repeat the statement while the condition is true.
Syntax/ example:
while true:
Try:
Print (“next value”)
Except for stop value:
Sys.exit()
3. Math functions
There are many math functions that can be used in Python 3 to get the values or result.
Syntax/ example:
cmp(x,y)
4. List values
In Python 3, the values can be listed in list form and access the result.
Syntax/ example:
list=[‘TOM, ‘JOHN’, ‘Latham’, ‘Mary’ ]
Print (“list[0]:”, list[0])
Output would be –‘TOM’
5. Functions
In Python 3, the function would be defined with keyword def
Syntax/ example:
def abc( str ):
“Hello world, my first function”
Print (str)
Return
6. Dictionary
In Python 3, it is used to assign the value and update the value as well.
Synatx/ example:
dict {‘Name’: ‘Tom’, ‘Salary’: 7500, ‘Location’: ‘USA’}
Print (“dict[‘Name’]:”, dict[‘Name’]) # output would be: Tom
Dict[‘salary’] = 8500 #updating the existing value.
Print (“dict[‘salary’]:”, dict[‘salary’])
Output would be 8500
7. Sending email
In python 3, smtp library object is being used for sending an email.
Syntax/ example:
import smtplib
Smtpobj = smtplib.SMTP([host [, port [, local_hostname]]])
8. Threading
In python 3, threading is used for various reasons and different threading method has been used like threading.activecount(), threading.currentthread(), threading.enumerate().
9. Regular expression
It is being used for matching the string from the number of strings. In this one of the example is the use of match function
Syntax/ example:
reg.match(pattern, string, flags = 0)
10. Database connectivity
In python 3, MySql is mainly used as a database and PyMySQL is an interface connecting to the MySQL database.
Syntax/ example:
import PyMySQL
db = PyMySQL.connect (“localhost”, “username”, “Password”, “Database name”)
Tips and Tricks to Use Python 3 Commands
- Use conditional operators wisely
- Use feature detection instead of using version detection
- Prevent compatibility regressions
- Check the dependencies while transitioning of the application to Python 3
- Use continuous integration to stay stable
- Use optional static type checking
- Update setup.py file to denote python 3 compatibility
- Use dictionary, list where ever needed
- Debug the scripts to check where is error or issue.
- Use enums
- Use python functions to get the result faster
- Always check the memory usage of objects
Conclusion
Python 3 makes the life comfortable for the programmers with introducing new features and compatibility to older versions as well. With the help of compatibility, the old applications can be easily moved in python 3 environment and make it up and running. Python 3 functions and its dynamic typing is really useful for the programming and make the performance better of the application.
Recommended Articles
This has been a guide to Python 3 Commands. Here we have discuss basic as well as advanced Python 3 Commands and some immediate Python 3 Commands. You may also look at the following article to learn more –