Reader small image

You're reading from  Practical Time Series Analysis

Product typeBook
Published inSep 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781788290227
Edition1st Edition
Languages
Right arrow
Authors (2):
Avishek Pal
Avishek Pal
author image
Avishek Pal

Dr. Avishek Pal, PhD, is a software engineer, data scientist, author, and an avid Kaggler living in Hyderabad, India. He achieved his Bachelor of Technology degree in industrial engineering from the Indian Institute of Technology (IIT) Kharagpur and earned his doctorate in 2015 from University of Warwick, Coventry, United Kingdom. He started his career as a software engineer at IBM India developing middleware solutions for telecom clients. This was followed by stints at a start-up product development company followed by Ericsson, the global telecom giant. After doctoral studies, Avishek started his career in India as a lead machine learning engineer for a leading US-based investment company. He is currently working at Microsoft as a senior data scientist. Avishek has published several research papers in reputed international conferences and journals.
Read more about Avishek Pal

PKS Prakash
PKS Prakash
author image
PKS Prakash

Dr. PKS Prakash is a data scientist and author. He has spent the last 12 years in developing many data science solutions in several practical areas in healthcare, manufacturing, pharmaceuticals, and e-commerce. He currently works as the data science manager at ZS Associates. He is the co-founder of Warwick Analytics, a spin-off from University of Warwick, UK. Prakash has published articles widely in research areas of operational research and management, soft computing tools, and advanced algorithms in leading journals such as IEEE-Trans, EJOR, and IJPR, among others. He has edited an article on Intelligent Approaches to Complex Systems and contributed to books such as Evolutionary Computing in Advanced Manufacturing published by WILEY and Algorithms and Data Structures using R and R Deep Learning Cookbook, published by PACKT.
Read more about PKS Prakash

View More author details
Right arrow

Chapter 6. Getting Started with Python

As you have chosen to read this book, we think that you might have a working knowledge of Python-if not, a hands-on expert who happens to live and breathe Python. In case you have a fair knowledge of Python at the least, you may choose to skip this appendix. If you are new to Python or looking for how to get started with the programming language, reading this appendix will help you get through the initial hurdles. It would also get you what you need to enjoy this book's chapters. So, without further ado, let's jump in!

Python is a general-purpose, high-level, and interpreted programming language, which appeared in 1991. Its creator, Guido van Rossum, started writing the interpretation of the language over the Christmas of 1989 and named the language after one of his favorite TV shows-Monty Python's Flying Circus.

Python emphasizes code readability through whitespace indentation to delimit code blocks rather than curly brackets, which are famously used...

Installation


Setting up Python on your computer would require installation of three components-the interpreter, an integrated development environment (IDE), and packages to support application development. Thankfully, we do not have to install these separately. There are software packages that bundle all three and a single installation makes the most available for use.

Python installers

There are two widely used options of installers that bundle the interpreter, an IDE, and useful Python packages:

  • Option 1: Installers that bundle all three components are available for download from https://www.python.org. This website hosts the installers for several different versions of the language. The latest versions, at the time of writing this book, are Python 3.2.6 and Python 2.7.13. The installers come with a basic IDE that is a good place to write some code and get a feel of how the language works. Several packages such as the ones on file handling, memory management, mathematical computations, and...

Basic data types


Python supports basic numeric data types such as int, long, and float, just like all major programming languages. None represents a null pointer in Python.

Strings are sequential data types in Python. Other sequential data types that are commonly used in Python are lists and tuples. The difference between a list and tuple is that the former is mutable while the latter is an immutable type. Therefore, the interpreter would throw an error if you try to modify a tuple. Let's dig a little deeper into lists and tuples.

List, tuple, and set

A list is a collection of elements. An element can be of any data type and a list can contain elements of different data types. A list has several important functions such as append, extend, insert, index, pop, and few others. The following table summarizes the functionality of these functions:

Keywords and functions


Keywords are reserved words that cannot be used as variable names. The following table gives the list of keywords and their purpose:

Function name

Functionality

append

Adds a new element to the end of the list.

extend

Adds new elements from an iterable. The members of the iterable element...

Iterators, iterables, and generators


In Python, we frequently encounter iterators, iterables, and generators as these are efficient ways of looping over a data type or data structure that is a sequence or a sequence can be created out of it. One clear advantage of using these looping techniques is that they require less memory. So, when you must access a sequence element by element, these techniques become very useful because a large sequence does not need to be loaded into memory all at once. For example, if you need to find the square of the first one trillion positive integers, there is no need to create a data structure to hold all numbers in memory at the same time. Iterators, iterables, and generators can be used to generate and process these numbers sequentially. Another example is processing a large text file. The entire file might not fit in memory. Hence, if we need to process the file, for example, to find word count per line of the file, we can iteratively loop over the lines...

Classes and objects


A class is a logical grouping of variables and functions. The class keyword is used in Python to define such logical groupings. A class often represents a real-life entity, for example, book, author, publishers, and so on. Entities have properties, which are represented by the variables defined in a class. Functions in a class, often referred to as methods, define how data about an instance of the entity can be captured and transformed. An instance of a class is a single realization of the entity. For example, book is an entity whereas Practical Time Series Analysis is an instance of book. To create instances, we initiate an object of a class. Object definition involves assigning values to the variables of the class through the constructor function. This job is done by the __init__ method that takes input and assigns them to class variables. The __init__ method can internally call other functions based on the logic of creating the object. Let's define a class about books...

Summary


This appendix covers the basics of the Python programming language. Topics such as data types, keywords, functions, classes, iterators, iterables, and generators have been discussed. These programming techniques form the building blocks of Python.

This book's chapters use several concepts and programming techniques that have been discussed here.

 

 

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Practical Time Series Analysis
Published in: Sep 2017Publisher: PacktISBN-13: 9781788290227
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Avishek Pal

Dr. Avishek Pal, PhD, is a software engineer, data scientist, author, and an avid Kaggler living in Hyderabad, India. He achieved his Bachelor of Technology degree in industrial engineering from the Indian Institute of Technology (IIT) Kharagpur and earned his doctorate in 2015 from University of Warwick, Coventry, United Kingdom. He started his career as a software engineer at IBM India developing middleware solutions for telecom clients. This was followed by stints at a start-up product development company followed by Ericsson, the global telecom giant. After doctoral studies, Avishek started his career in India as a lead machine learning engineer for a leading US-based investment company. He is currently working at Microsoft as a senior data scientist. Avishek has published several research papers in reputed international conferences and journals.
Read more about Avishek Pal

author image
PKS Prakash

Dr. PKS Prakash is a data scientist and author. He has spent the last 12 years in developing many data science solutions in several practical areas in healthcare, manufacturing, pharmaceuticals, and e-commerce. He currently works as the data science manager at ZS Associates. He is the co-founder of Warwick Analytics, a spin-off from University of Warwick, UK. Prakash has published articles widely in research areas of operational research and management, soft computing tools, and advanced algorithms in leading journals such as IEEE-Trans, EJOR, and IJPR, among others. He has edited an article on Intelligent Approaches to Complex Systems and contributed to books such as Evolutionary Computing in Advanced Manufacturing published by WILEY and Algorithms and Data Structures using R and R Deep Learning Cookbook, published by PACKT.
Read more about PKS Prakash

Keywords

Explanation

False

Boolean false value:

>>bool_var = False

True

Boolean true value:

>>bool_var = True

and

Logical operator that returns True only if both the operands are True or evaluates to True:

>>a = 2

>>if a > 0:

print(a)

>>2

as

Creates an alias for a module that is being imported:

import pandas as pd

import scikit-learn as skl

assert

Used to evaluate a logical expression to check values of variables at runtime and raises an AssertionError if the expression evaluates to False:

>>a = -2

>> assert a > 0

The preceding assert keyword raises an AssertionError.

break

Used to exit a loop such as forloop or while loop when a condition is met.

class

Keyword that indicates a class declaration.

continue

Indicates the interpreter to move to the next iteration in a for or while loop without executing the code in the...