www.t u tori a I sp oin t. com
y https://twitter.com/tutoriaTsq9int
About the Tutorial
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). Python is named after a TV Show called Monty Pythons Flying Circus and not after Python-the snake.
Python 3.0 was released in 2008. Although this version is supposed to be backward incompatibles, later on many of its important features have been backported to be compatible with the version 2.7. This tutorial gives enough understanding on Python 3 version programming language. Please refer to this link for our Python 2 tutorial.
Audience
This tutorial is designed for software programmers who want to upgrade their Python skills to Python 3. This tutorial can also be used to learn Python programming language from scratch.
Prerequisites
You should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages is a plus.
Execute Python Programs
For most of the examples given in this tutorial you will find Try it option, so just make use of it and enjoy your learning.
Try the following example using Try it option available at the top right corner of the below sample code box
#!/us r /bi n /py t hon3 print ("H e llo, Py t hon! " ) |
Table of Contents
Python 3 Basic Tutorial
Python 3.x introduced some Python 2-incompatible keywords and features that can be imported via the in-built future module in Python 2. It is recommended to use
future imports, if you are planning Python 3.x support for your code.
For example, if we want Python 3.x's integer division behavior in Python 2, add the following import statement.
from future import division |
The print Function
Most notable and most widely known change in Python 3 is how the print function is used. Use of parenthesis () with print function is now mandatory. It was optional in Python 2.
print "He l lo W orl d " #is accep t able in Pyt h on 2 print ("H e llo Wor l d") # in Py t hon 3, p rint must be fo l low e d by () |
The print() function inserts a new line at the end, by default. In Python 2, it can be suppressed by putting ',' at the end. In Python 3, "end=' '" appends space instead of newline.
print x, # T ra iling com m a s u ppr e sses n ewline in Pyt h on 2 print ( x, e nd=" ") # A pp ends a sp a ce i nst e ad of a new l ine in P yth o n 3 |
Reading Input from Keyboard
Python 2 has two versions of input functions, input() and raw_input() . The input() function treats the received data as string if it is included in quotes '' or "", otherwise the data is treated as number.
In Python 3, raw_input() function is deprecated. Further, the received data is always treated as string.
>>> x '10' >>> x=raw_inpu t ("something: " ) something:10 #entered data is treated as string even without '' >>> x '10' >>> x = raw _ inp u t(" s omet hi ng:") somet h ing : '10' #e n tered data t rea t ed a s s t ring i nclud i ng ' ' >>> x "'10'" In Py t hon 3 >>> x = inp u t(" s ome t hing : " ) somet h ing : 10 >>> x '10' >>> x = inp u t(" s ome t hing : " ) somet h ing : '10' #e n tered data t rea t ed a s s t ring w ith or wi t hout '' >>> x "'10'" >>> x = raw _ inp u t(" s omet hi ng:") # w i ll r esu l t Na me Error Trace b ack (mo s t r e cent c all l a st): File "", li n e 1, in x = raw _ inp u t(" s omet hi ng:") NameE r ror: na m e ' r aw_i np ut' is not de f ined |
Integer Division
In Python 2, the result of division of two integers is rounded to the nearest integer. As a result, 3/2 will show 1. In order to obtain a floating-point division, numerator or denominator must be explicitly used as float. Hence, either 3.0/2 or 3/2.0 or 3.0/2.0 will result in 1.5
Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.
Unicode Representation
Python 2 requires you to mark a string with a u if you want to store it as Unicode.
Python 3 stores strings as Unicode, by default. We have Unicode (utf-8) strings, and 2 byte classes: byte and byte arrays.