Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Scientific Computing with Python 3
Scientific Computing with Python 3

Scientific Computing with Python 3: An example-rich, comprehensive guide for all of your Python computational needs

Arrow left icon
Profile Icon Führer Profile Icon Claus Fuhrer Profile Icon Solem Profile Icon Verdier
Arrow right icon
$9.99 $43.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (2 Ratings)
eBook Dec 2016 332 pages 1st Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Führer Profile Icon Claus Fuhrer Profile Icon Solem Profile Icon Verdier
Arrow right icon
$9.99 $43.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (2 Ratings)
eBook Dec 2016 332 pages 1st Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Scientific Computing with Python 3

Chapter 2. Variables and Basic Types

In this chapter, we will present the most important and basic types in Python. What is a type? It is a set consisting of data content, its representation, and all possible operations. Later in this book, we will make this definition much more precise, when we introduce the concepts of a class in Chapter 8, Classes.

Variables

Variables are references to Python objects. They are created by assignments, for example:

a = 1 
diameter = 3.
height = 5.
cylinder = [diameter, height] # reference to a list

Variables take names that consist of any combination of capital and small letters, the underscore _ , and digits. A variable name must not start with a digit. Note that variable names are case sensitive. A good naming of variables is an essential part of documenting your work, so we recommend that you use descriptive variable names.

Python has some reserved keywords, which cannot be used as variable names (refer to following table, Table 2.1). An attempt to use such a keyword as variable name would raise a syntax error.

Variables

Table 2.1: Reserved Python keywords.

As opposed to other programming languages, variables require no type declaration. You can create several variables with a multiple assignment statement:

a = b = c = 1   # a, b and c get the same value 1

Variables can also be altered after their definition:

a...

Numeric types

At some point, you will have to work with numbers, so we start by considering different forms of numeric types in Python. In mathematics, we distinguish between natural numbers (ℕ), integers (ℤ), rational numbers (ℚ), real numbers (ℝ) and complex numbers (ℂ). These are infinite sets of numbers. Operations differ between these sets and may even not be defined. For example, the usual division of two numbers in ℤ might not result in an integer — it is not defined on ℤ.

In Python, like many other computer languages, we have numeric types:

  • The numeric type int, which is at least theoretically the entire ℤ
  • The numeric type float, which is a finite subset of ℝ and
  • The numeric type complex, which is a finite subset of ℂ

Finite sets have a smallest and a largest number and there is a minimum spacing between two numbers; refer to the section on Floating Point Representation for further details.

Integers

The...

Booleans

Boolean is a datatype named after George Boole (1815-1864). A Boolean variable can take only two values, True or False. The main use of this type is in logical expressions. Here are some examples:

a = True 
b = 30 > 45   # b gets the value False

Boolean expressions are often used in conjunction with the if statement:

if x > 0:
   print("positive")
else:
   print("nonpositive)

Boolean operators

Boolean operations are performed using the and, or, and not keywords in Python:

True and False # False
False or True # True
(30 > 45) or (27 < 30) # True
not True # False
not (3 > 4) # True

The operators follow some precedence rules (refer to section Executing scripts in Chapter 1, Getting started) which would make the parentheses in the third line and in the last obsolete (it is a good practice to use them anyway to increase the readability of your code). Note that the and operator is implicitly chained in the following Boolean expressions...

Strings

The type string is a type used for text:

name = 'Johan Carlsson'
child = "Åsa is Johan Carlsson's daughter"
book = """Aunt Julia 
       and the Scriptwriter"""

A string is enclosed either by single or double quotes. If a string contains several lines, it has to be enclosed by three double quotes """ or three single quotes '''.

Strings can be indexed with simple indexes or slices (refer to Chapter 3, Container Types, for a comprehensive explanation on slices):

book[-1] # returns 'r' 
book[-12:] # returns 'Scriptwriter'

Strings are immutable; that is, items cannot be altered. They share this property with tuples. The command book[1] = 'a' returns:

TypeError: 'str' object does not support item assignment

The string '\n' is used to insert a line break  and 't' inserts a horizontal tabulator (TAB) into the string to align several lines...

Variables


Variables are references to Python objects. They are created by assignments, for example:

a = 1 
diameter = 3.
height = 5.
cylinder = [diameter, height] # reference to a list

Variables take names that consist of any combination of capital and small letters, the underscore _ , and digits. A variable name must not start with a digit. Note that variable names are case sensitive. A good naming of variables is an essential part of documenting your work, so we recommend that you use descriptive variable names.

Python has some reserved keywords, which cannot be used as variable names (refer to following table, Table 2.1). An attempt to use such a keyword as variable name would raise a syntax error.

Table 2.1: Reserved Python keywords.

As opposed to other programming languages, variables require no type declaration. You can create several variables with a multiple assignment statement:

a = b = c = 1   # a, b and c get the same value 1

Variables can also be altered after their...

Numeric types


At some point, you will have to work with numbers, so we start by considering different forms of numeric types in Python. In mathematics, we distinguish between natural numbers (ℕ), integers (ℤ), rational numbers (ℚ), real numbers (ℝ) and complex numbers (ℂ). These are infinite sets of numbers. Operations differ between these sets and may even not be defined. For example, the usual division of two numbers in ℤ might not result in an integer — it is not defined on ℤ.

In Python, like many other computer languages, we have numeric types:

  • The numeric type int, which is at least theoretically the entire ℤ
  • The numeric type float, which is a finite subset of ℝ and
  • The numeric type complex, which is a finite subset of ℂ

Finite sets have a smallest and a largest number and there is a minimum spacing between two numbers; refer to the section on Floating Point Representation for further details.

Integers

The simplest numerical type is the integer type.

Plain integers

The statement k = 3 assigns...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Your ultimate resource for getting up and running with Python numerical computations
  • Explore numerical computing and mathematical libraries using Python 3.x code with SciPy and NumPy modules
  • A hands-on guide to implementing mathematics with Python, with complete coverage of all the key concepts

Description

Python can be used for more than just general-purpose programming. It is a free, open source language and environment that has tremendous potential for use within the domain of scientific computing. This book presents Python in tight connection with mathematical applications and demonstrates how to use various concepts in Python for computing purposes, including examples with the latest version of Python 3. Python is an effective tool to use when coupling scientific computing and mathematics and this book will teach you how to use it for linear algebra, arrays, plotting, iterating, functions, polynomials, and much more.

Who is this book for?

This book is for anyone who wants to perform numerical and mathematical computations in Python. It is especially useful for developers, students, and anyone who wants to use Python for computation. Readers are expected to possess basic a knowledge of scientific computing and mathematics, but no prior experience with Python is needed.

What you will learn

  • The principal syntactical elements of Python
  • The most important and basic types in Python
  • The essential building blocks of computational mathematics, linear algebra, and related Python objects
  • Plot in Python using matplotlib to create high quality figures and graphics to draw and visualize your results
  • Define and use functions and learn to treat them as objects
  • How and when to correctly apply object-oriented programming for scientific computing in Python
  • Handle exceptions, which are an important part of writing reliable and usable code
  • Two aspects of testing for scientific programming: Manual and Automatic

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 23, 2016
Length: 332 pages
Edition : 1st
Language : English
ISBN-13 : 9781786463647
Category :
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Dec 23, 2016
Length: 332 pages
Edition : 1st
Language : English
ISBN-13 : 9781786463647
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 164.97
Artificial Intelligence with Python
$54.99
Scientific Computing with Python 3
$54.99
Bayesian Analysis with Python
$54.99
Total $ 164.97 Stars icon
Banner background image

Table of Contents

16 Chapters
1. Getting Started Chevron down icon Chevron up icon
2. Variables and Basic Types Chevron down icon Chevron up icon
3. Container Types Chevron down icon Chevron up icon
4. Linear Algebra – Arrays Chevron down icon Chevron up icon
5. Advanced Array Concepts Chevron down icon Chevron up icon
6. Plotting Chevron down icon Chevron up icon
7. Functions Chevron down icon Chevron up icon
8. Classes Chevron down icon Chevron up icon
9. Iterating Chevron down icon Chevron up icon
10. Error Handling Chevron down icon Chevron up icon
11. Namespaces, Scopes, and Modules Chevron down icon Chevron up icon
12. Input and Output Chevron down icon Chevron up icon
13. Testing Chevron down icon Chevron up icon
14. Comprehensive Examples Chevron down icon Chevron up icon
15. Symbolic Computations - SymPy Chevron down icon Chevron up icon
References Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(2 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
D. A. Robinson Jul 08, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I have been using Python 2.7 on all my projects so far but decided to upgrade to Python 3 for all subsequent projects, and needed a text book to get to know where the important differences are. So far this seems an adequate tome for this task
Amazon Verified review Amazon
Tim Y Oct 14, 2021
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book can help you get started rapidly with using Python in scientific computation. It has detailed exposition on the basic syntax and data structures of Python. It also consists of advanced contents such as error handling, symbolic calculations and parallel computations. However, I wish it would focus more on numerical algorithms such as equation solving, matrix diagonalization, optimization, interpolation etc. Overall, it's a great book for those who are beginners in scientific computation with Python.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.