PYTHON PROGRAMMING
The Ultimate Expert Guide: Advanced Features, Object-Oriented Programming, Data Analysis, Artificial Intelligence and Machine Learning with Python
Copyright 2019 by Clive Campbell - All rights reserved.
The content contained within this book may not be reproduced, duplicated or transmitted without direct written permission from the author or the publisher.
Under no circumstances will any blame or legal responsibility be held against the publisher, or author, for any damages, reparation, or monetary loss due to the information contained within this book. Either directly or indirectly.
Legal Notice:
This book is copyright protected. This book is only for personal use. You cannot amend, distribute, sell, use, quote or paraphrase any part, or the content within this book, without the consent of the author or publisher.
Disclaimer Notice:
The information contained in this book has been well-researched and correct during the time it as written but it should not be misconstrued as perfect and flawless. The programming language, Python, and its functionality, are gravely dependent on the specifications of the device and computer to which it has been installed. Moreover, Python, similar to other programming languages, is constantly upgraded. The publisher disclaims any liability for any form of inconsistencies due to software upgrade or damage to the device wherein the software was installed, or any other errors, whether such errors have been deliberate or caused by negligence, misunderstanding of the procedures, or accidents.
Table of Contents
Introduction
Is this your first time programming? If not, then we assume that you want to learn how to use machine learning with Python. Also, you could be looking for information about why and how you can get started with Python. If you are an expert programmer in any language, it will be easy for you to pick up Python very fast.
Python refers to a high-level programming language that is dynamic. It is easy to learn and supports powerful typing. Python programs are very natural, in that it is easy to understand and read them (thanks to the exclusion of braces and semicolons). Python programming language can run on any computer platform, ranging from Linux to Windows to Solaris, and Macintosh, et cetera. The simple nature of Python is what makes it popular and a perfect choice for computer programmers. The following points give a highlight of its features:
It is a highly readable programming language
It has a clean visual layout
Less syntactic exceptions
Excellent for rapid application and scripting
It supports dynamic and elegant typing
It can easily be interpreted
It is compatible with many platforms
It has a clean and perfect visual layout
It is a very popular language in many fields such as Artificial Intelligence, Big Data, and Automation.
Before you can get started in Python, you need to know how to install Python on your computer. Next, you need to know a few concepts about the language syntax to be able to read and understand the Python code. To learn more about installation and syntax, move to the next page.
Chapter 1: Basics of the Python Programming Language
Before you can get started with Machine Learning with Python, you must have Python installed on your computer; however, you might not need to download it.
So, the first thing to do is to confirm that Python is not installed by typing Python in a command window. When you see a response from a Python interpreter, it will consist of a version number in its original display. In general, any recent version will work because Python tries to maintain backward compatibility.
If you want to install Python, you might as well search for the most recent stable version. This version will have the highest number not marked as an alpha or beta release.
First Python Progra m
Usually, when you start to learn any programming language, your first program to write will be Hello, World!. This is an easy program that prints Hello, World! For our first Python program, you will learn how to write a program that adds two numbers.
A program that adds two numbers
In the coding that we have above, we are basically telling the compiler that we want to add together the number 3 and 5, so the output that we are going to get from this, if it is set up well, is 8. Lets look at this a bit more closely too:
The first line of the program begins with a comment. Comments in Python programming are written starting with #. Python interpreters and compilers ignore comments. The reason why comments should be applied in Python programming is to describe the function of the code. Furthermore, comments help any other programmer to understand the working of your code .
Variables and Data Types
A data type, as the name suggests, is the category of data. It defines a collection of values plus operations that can take place on those values. The explicit value used in our programs is literal. For instance, 11, 30.22, pypi are all literals. Each literal has a type linked to it. Please see the following example, 11 is an int type, 30.22 is a float type and pypi is of type string. Often, the type of literal will determine the type of operations that can be done to it. The table below contains basic data types in Python.
Python contains an in-built function called type () which is used to define the data type of the literal.
The describes that the type of 54 is int. Also, and shows that a string and 98. 188 is of type str and float respectively. At first, you might say that 3.14 is of type float but since it is wrapped inside double quotes, it is definitely a string. In the same way, 99 is a string.
A sequence of character data is a string. The string type in Python is called str. String literals can either be defined by single or double-quotes. All the characters inside the opening and closing quotes are part of the string as shown below:
>>> ''
' '
Escape sequence in strings
There are times when you want Python to interpret a sequence of characters inside a string differently. This might happen in one or two ways:
If you want to suppress the unique interpretation that specific characters are supplied within a string.
You want to apply specific interpretation to characters contained in a string that is often taken literally.
You can do this by using the backslash (\) character. A backslash character in a string implies that one or more characters that follow it must be uniquely treated. This is called an escape sequence because the backslash will make subsequent character sequence to escape its normal meaning.