CHAPTER 1: PYTHON BASICS
1. Entering expressions into the interactive shell
2. The integer, floating-point and String Data Types
3. String concatenation and replication
4. Storing values in variables
5. Your first program
6. Dissecting your program
1.1. Entering expressions into the interactive shell
Run the interactive shell by launching IDLE, which is installed with Python. On
Windows, open the Start menu, select All Programs ▸ Python 3.3, and then select
IDLE (Python GUI). On OS X, select Applications ▸ MacPython 3.3 ▸ IDLE. On
Ubuntu, open a new Terminal window and enter idle3.
A window with the >>> prompt should appear; that‘s the interactive shell.
>>> 2+2
4
The IDLE window should now show some text like this: Python 3.3.2
(v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on
win32
Type "copyright", "credits" or "license()" for more information.
>>> 2+2
4
In Python, 2 + 2 is called an expression, which is the most basic kind of programming
instruction in the language. Expressions consist of values (such as 2) and operators
(such as +), and they can always evaluate (that is, reduce) down to a single value.
That means you can use expressions anywhere in Python code that you could also use
a value.
Manjushree T L , Asst Prof., Dept of ISE,SVIT Page 1
,Introduction to Python Programming (BPLCK105B) Module 1
In the previous example, 2 + 2 is evaluated down to a single value, 4. A single value
with no operators is also considered an expression, though it evaluates only to itself,
as shown here:
>>> 2+2
4
The other operators which can be used are:
The order of operations (also called precedence) of Python math operators is similar
to that of mathematics. The ** operator is evaluated first; the *, /, //, and % operators
are evaluated next, from left to right; and the + and - operators are evaluated last (also
from left to right). We can use parentheses to override the usual precedence if you
need to.
Manjushree T L , Asst Prof., Dept of ISE,SVIT Page 2
,Introduction to Python Programming (BPLCK105B) Module 1
Due to wrong instructions errors occurs as shown below:
1.2 The integer, floating-point and String Data Types
The expressions are just values combined with operators, and they always evaluate
down to a single value.
A data type is a category for values, and every value belongs to exactly one data type.
-
The integer (or int) data type indicates values that are whole numbers.
Numbers with a decimal point, such as 3.14, are called floating-point numbers (or
floats).
Note that even though the value 42 is an integer, the value 42.0 would be a floating-
point number.
Python programs can also have text values called strings, or strs and surrounded in
single quote.
The string with no characters, '', called a blank string.
Manjushree T L , Asst Prof., Dept of ISE,SVIT Page 3
, Introduction to Python Programming (BPLCK105B) Module 1
If the error message SyntaxError: EOL while scanning string literal, then probably
the final single quote character at the end of the string is missing.
1.3 String concatenation and replication
The meaning of an operator may change based on the data types of the values next to
it
For example, + is the addition operator when it operates on two integers or floating-
point values.
However, when + is used on two string values, it joins the strings as the string
concatenation operator.
If we try to use the + operator on a string and an integer value, Python will not know
how to handle this, and it will display an error message.
The * operator is used for multiplication when it operates on two integer or floating-
point values.
But, when the * operator is used on one string value and one integer value; it becomes
the string replication operator.
Manjushree T L , Asst Prof., Dept of ISE,SVIT Page 4