Introduction to Python 3 Commands
Python 3 commands have been introduced with Python 2 features, and it is compatible with python 2 with the help of using some inbuilt keywords 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 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 command, the entered data is always treated as a 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 a range function in python 3
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 the keyword ‘as’
Syntax/ example:
except issue an error
7. Next function
In python 3, the next function is being used, and the .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 are 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 stopvalue:
Sys.exit()
3. Math functions
There are many math functions that can be used in Python 3 to get the values or results.
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])
The output would be –‘TOM.’
5. Functions
In Python 3, the function would be defined with the 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, the 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 the 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 an error or issue.
- Use enums
- Use python functions to get the result faster
- Always check the memory usage of objects
Conclusion
Python 3 makes life comfortable for the programmers by introducing new features and compatibility to older versions as well. With the help of compatibility, the old applications can be easily moved in the python 3 environment and make it up and running. Python 3 functions and its dynamic typing are really useful for the programming and make the application’s performance better.
Recommended Articles
This has been a guide to Python 3 Commands. Here we have discussed 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 –