Reader small image

You're reading from  Scientific Computing with Python - Second Edition

Product typeBook
Published inJul 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781838822323
Edition2nd Edition
Languages
Right arrow
Authors (3):
Claus Führer
Claus Führer
author image
Claus Führer

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.
Read more about Claus Führer

View More author details
Right arrow

2.1 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 33 reserved keywords, which cannot be used as variable names (see Table 2.1). Any attempt to use such a keyword as a variable name would raise a syntax error:

Table 2.1: Reserved Python keywords

As opposed to other programming languages, variables require no type declaration in Python. The type is automatically deduced:

x = 3 # integer (int)
y = 'sunny' # string (str)

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 = 1 
a = a + 1 # a gets the value 2
a = 3 * a # a gets the value 6

The last two statements can be written by combining the two operations with an assignment directly by using increment operators:

a += 1 # same as a = a + 1 
a *= 3 # same as a = 3 * a
Previous PageNext Page
You have been reading a chapter from
Scientific Computing with Python - Second Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781838822323
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 (3)

author image
Claus Führer

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.
Read more about Claus Führer