Python 2 to Python 3: A Comprehensive Guide to Porting Your Code, Study notes of Programming Paradigms

simple tips to open the begginer in learning python

Typology: Study notes

2019/2020

Uploaded on 12/08/2020

lasethume-daudi
lasethume-daudi 🇰🇪

2 documents

1 / 459

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Python 2 to Python 3: A Comprehensive Guide to Porting Your Code and more Study notes Programming Paradigms in PDF only on Docsity!

How To Code in Python 3

Lisa Tagliaferri

DigitalOcean, New York City, New York, USA

About DigitalOcean

DigitalOcean is a cloud services platform delivering the simplicity developers love and businesses trust to run production applications at scale. It provides highly available, secure and scalable compute, storage and networking solutions that help developers build great software faster. Founded in 2012 with offices in New York and Cambridge, MA, DigitalOcean offers transparent and affordable pricing, an elegant user interface, and one of the largest libraries of open source resources available. For more information, please visit https://www.digitalocean.com or follow @digitalocean on Twitter. Read this book online and receive server credit via https://do.co/python-book.

DigitalOcean Community Team

Director of Community: Etel Sverdlov Technical Writers: Melissa Anderson, Brian Boucheron, Mark Drake, Justin Ellingwood, Katy Howard, Lisa Tagliaferri Technical Editors: Brian Hogan, Hazel Virdó

How To Code in Python 3

  1. Introduction
  2. Python 2 vs Python 3 : Practical Considerations
  3. How To Install Python 3 and Set Up a Local Programming Environment on Ubuntu 16. 04
  4. How To Install Python 3 and Set Up a Local Programming Environment on macOS
  5. How To Install Python 3 and Set Up a Local Programming Environment on Windows 10
  6. How To Install Python 3 and Set Up a Local Programming Environment on CentOS 7
  7. How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 16. 04 Server
  8. How To Write Your First Python 3 Program
  9. How To Work with the Python Interactive Console
  10. How To Write Comments
  11. Understanding Data Types
  12. An Introduction to Working with Strings
  13. How To Format Text
  14. An Introduction to String Functions
  15. How To Index and Slice Strings
  16. How To Convert Data Types
  17. How To Use Variables
  18. How To Use String Formatters
  19. How To Do Math with Operators
  20. Built-in Python 3 Functions for Working with Numbers

Introduction

Why Learn To Code

Software and technology are becoming increasingly integrated into our everyday lives, allowing us to accomplish tasks, navigate to destinations, make purchases, and stay connected with friends. Because of how pervasive software now is to the human experience, it is important for all of us to learn some of the key foundational elements of computer programming. While some may choose to study computer science as part of their formal education, everyone can benefit from an understanding of algorithmic thinking and computational processes. Learning how the software that we use on a daily basis is made can allow us as end users to evaluate how and why these applications are developed, enabling us to think critically about these tools and how to improve them. Just like any other product, computer programs are designed and developed by people who have unconscious biases, make errors, and may not be considering all aspects of a problem they are trying to solve. Though development teams may do thorough testing and work to create sophisticated and useful programs, they do not always meet the needs and expectations of all users. While not everyone needs to learn to code complex programs, learning how coding works can help shape the future of technology and increase the number of stakeholders, decision makers, and knowledge producers who can work to build better software for everyone. Some of us may choose to solve challenging problems within the technology sector, but for those of us not working in computer science, a

programming background can still be a great asset to our professional fields. Computer programming provides many applications across domains, and can help us solve problems in specialities such as medicine, economics, sociology, history, and literature, to name a few. By integrating technology’s methodologies into our own fields, we can leverage computational logic and software design and development practices in our work. When we synthesize knowledge across spheres and collaborate with people from different backgrounds, we can innovate in new, more inclusive ways that can enact meaningful impact across many communities.

Why Learn Python

Extremely versatile and popular among developers, Python is a good general-purpose language that can be used in a variety of applications. For those with an understanding of English, Python is a very human- readable programming language, allowing for quick comprehension. Because Python supports multiple styles including scripting and object- oriented programming, it is considered to be a multi-paradigm language that enables programmers to use the most suitable style to complete a project. Increasingly used in industry, Python offers a lot of potential for those who would like to begin coding while also being a good choice for those looking to pick up an additional programming language. Learning the key concepts of Python can help you understand how programs work while also imparting foundational logic that can serve you in other domains. Understanding what Python and computer programming can offer you both as a user and as a developer is important as technology is further integrated into daily life.

When you are done with the book, we encourage you to look at project-based tutorials to put your knowledge into play while creating projects that can help you solve problems. While you are working on these projects, you can continue to refer to the chapters in this book as reference material. As part of your learning process and once you feel comfortable, we recommend that you contribute to an open-source project to improve programs and drive greater access via software and technical documentation pull requests or repository maintenance. Our community is bigger than just us and building software together can make sure that everyone has an opportunity to participate in the technology we use every day.

Python 2 vs Python 3 : Practical

Considerations

Python is an extremely readable and versatile programming language. With a name inspired by the British comedy group Monty Python, it was an important foundational goal of the Python development team to make the language fun to use. Easy to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners. As Python is a multiparadigm language — that is, it supports multiple programming styles including scripting and object-oriented — it is good for general purpose use. Increasingly used in industry by organizations such as United Space Alliance (NASA’s main shuttle support contractor), and Industrial Light & Magic (the VFX and animation studio of Lucasfilm), Python offers a lot of potential for those looking to pick up an additional programming language. Developed in the late 1980 s and first published in 1991 , Python was authored by Guido van Rossum, who is still very active in the community. Conceived as a successor to the ABC programming language, Python’s first iteration already included exception handling, functions, and classes with inheritance. When an important Usenet newsgroup discussion forum called comp.lang.python was formed in 1994 , Python’s user base grew, paving the way for Python to become one of the most popular programming languages for open source development.

General Overview

At first, Python 3 was slowly adopted due to the language not being backwards compatible with Python 2 , requiring people to make a decision as to which version of the language to use. Additionally, many package libraries were only available for Python 2 , but as the development team behind Python 3 has reiterated that there is an end of life for Python 2 support, more libraries have been ported to Python 3. The increased adoption of Python 3 can be shown by the number of Python packages that now provide Python 3 support, which at the time of writing includes 339 of the 360 most popular Python packages. Python 2. 7 Following the 2008 release of Python 3. 0 , Python 2. 7 was published on July 3 , 2010 and planned as the last of the 2 .x releases. The intention behind Python 2. 7 was to make it easier for Python 2 .x users to port features over to Python 3 by providing some measure of compatibility between the two. This compatibility support included enhanced modules for version 2. 7 like unittest to support test automation, argparse for parsing command-line options, and more convenient classes in collections. Because of Python 2. 7 ’s unique position as a version in between the earlier iterations of Python 2 and Python 3. 0 , it has persisted as a very popular choice for programmers due to its compatibility with many robust libraries. When we talk about Python 2 today, we are typically referring to the Python 2. 7 release as that is the most frequently used version. Python 2. 7 , however, is considered to be a legacy language and its continued development, which today mostly consists of bug fixes, will cease completely in 2020.

Key Differences

While Python 2. 7 and Python 3 share many similar capabilities, they should not be thought of as entirely interchangeable. Though you can write good code and useful programs in either version, it is worth understanding that there will be some considerable differences in code syntax and handling. Below are a few examples, but you should keep in mind that you will likely encounter more syntactical differences as you continue to learn Python. Print In Python 2 , print is treated as a statement instead of a function, which was a typical area of confusion as many other actions in Python require arguments inside of parentheses to execute. If you want your console to print out Sammy the Shark is my favorite sea creature in Python 2 you can do so with the following print statement: print "Sammy the Shark is my favorite sea creature" With Python 3 , print() is now explicitly treated as a function, so to print out the same string above, you can do so simply and easily using the syntax of a function: print("Sammy the Shark is my favorite sea creature") This change made Python’s syntax more consistent and also made it easier to change between different print functions. Conveniently, the

To override this, you could add decimal places as in 5. 0 / 2. 0 to get the expected answer 2. 5. In Python 3 , integer division became more intuitive, as in: a = 5 / 2 print(a) Output

  1. 5 You can still use 5. 0 / 2. 0 to return 2. 5 , but if you want to do floor division you should use the Python 3 syntax of //, like this: b = 5 // 2 print(b) Output 2 This modification in Python 3 made dividing by integers much more intuitive and is a feature that is not backwards compatible with Python

Unicode Support When programming languages handle the string type — that is, a sequence of characters — they can do so in a few different ways so that computers can convert numbers to letters and other symbols.

Python 2 uses the ASCII alphabet by default, so when you type "Hello, Sammy!" Python 2 will handle the string as ASCII. Limited to a couple of hundred characters at best in various extended forms, ASCII is not a very flexible method for encoding characters, especially non- English characters. To use the more versatile and robust Unicode character encoding, which supports over 128 , 000 characters across contemporary and historic scripts and symbol sets, you would have to type u"Hello, Sammy!", with the u prefix standing for Unicode. Python 3 uses Unicode by default, which saves programmers extra development time, and you can easily type and display many more characters directly into your program. Because Unicode supports greater linguistic character diversity as well as the display of emojis, using it as the default character encoding ensures that mobile devices around the world are readily supported in your development projects. If you would like your Python 3 code to be backwards-compatible with Python 2 , though, you can keep the u before your string. Continued Development The biggest difference between Python 3 and Python 2 is not a syntactical one, but the fact that Python 2. 7 will lose continued support in 2020 and Python 3 will continue to be developed with more features and more bug fixes. Recent developments have included formatted string literals, simpler customization of class creation, and a cleaner syntactical way to handle matrix multiplication. Continued development of Python 3 means that developers can rely on having issues fixed in a timely manner, and programs can be more

starting out. You can learn more about this process by reading the tutorial How To Port Python 2 Code to Python 3. It is important to keep in mind that as more developer and community attention focuses on Python 3 , the language will become more refined and in-line with the evolving needs of programmers, and less support will be given to Python 2. 7.

How To Install Python 3 and Set Up a Local

Programming Environment on Ubuntu 16. 04

This tutorial will get you up and running with a local Python 3 programming environment in Ubuntu 16. 04. Python is a versatile programming language that can be used for many different programming projects. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Easy to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python. This tutorial will guide you through installing Python 3 on your local Linux machine and setting up a programming environment via the command line. This tutorial will explicitly cover the installation procedures for Ubuntu 16. 04 , but the general principles apply to any other distribution of Debian Linux.

Prerequisites

You will need a computer with Ubuntu 16. 04 installed, as well as have administrative access to that machine and an internet connection.

Step 1 — Setting Up Python 3

We’ll be completing our installation and setup on the command line, which is a non-graphical way to interact with your computer. That is,