3
Python basics
Contents
Introducing the fundamentals
Getting started
Whitespace matters
Using variables
Simple data types
Arithmetic
String manipulation
Collection data types
List and tuple manipulation
Set manipulation
Dictionary manipulation
Importing modules
Introducing the fundamentals
Python is a powerful, general-purpose computing language. It can be used for large and
complicated tasks or for small and simple ones. Naturally, to get people started with its
use, we begin with relatively straightforward examples and then afterwards increase the
complexity. Hence, in the next two chapters we cover most of the day-to-day
fundamentals of the language. You will need to be, at least a little, familiar with these
ideas to appreciate the subsequent chapters. Much of what we illustrate here is called
scripting, although there is no hard and fast rule about what is deemed to be a program
and what is ‘merely’ a script. We will use the terminology interchangeably.
Here we describe most of the common operations and the basic types of data, but some
aspects will be left to dedicated chapters. Initially the focus will be on the core data types
handled by Python, which basically means numbers and text. With numbers we will of
course describe doing arithmetic operations and how this can be moved from the specific
into the abstract using variables. All the other kinds of data in Python can also be worked
with in a similarly abstract manner, although the operations that are used to manipulate
non-numeric data won’t be mathematical. Moving on from simple numbers and text we
will describe some of the other standard types of Python data. Most notable of these are
the collection types that act as containers for other data, so, for example, we could have a
list of words or a set of numbers, and the list or set is a named entity in itself; just another
item that we can give a label to in our programs. Python also has the ability to let you
describe your own types of data, by making an object specification called a class.
However, this will be discussed in
Chapter 7
. We will end this chapter by introducing the
idea of importing Python modules, which is a mechanism to allow a program to access
extra functionality contained in separate files.
Finally, using Python is not only about the operation of programs, it is also important to
consider what it means to the people who read it. Hopefully you will be writing clearly
understandable code, with meaningful variable names and such like. Nonetheless, it is a
good idea, when using any programming language, to get into the habit of adding human-
readable comments to your programs, especially at points where the logic of what is
happening is not so obvious. Such comments are simply textual descriptions that are
separate from the functional part of the code. In Python, comments are usually introduced
using the hash symbol
1
‘#’, whereupon all subsequent text on that line is for humans to
read and not part of the program proper.
Do'stlaringiz bilan baham: |