PYTHON BASICS
MODULE -1
Introduction and Flow control
Introduction, Python Basics: Entering Expressions into the Interactive Shell, The
Integer, Floating-Point, and String Data Types, String Concatenation and Replication, Storing
Values in Variables, Your First Program, Dissecting your Program
Flow control: Boolean Values, Comparison Operators, Boolean Operators, Mixing
Boolean and Comparison Operators, Elements of Flow Control, Program Execution, Flow
Control Statements, Importing Modules, Ending a Program Early with sys.exit()
Text Book: T1 Chapters: 1, 2
ENTERING EXPRESSIONS INTO
THE INTERACTIVE SHELL
ENTERING EXPRESSIONS INTO THE
INTERACTIVE SHELL
A >>> prompt appears in
the interactive shell
In the given example
2 + 2 is called an
expression
2 and 2 are values
+ is an operator
The expression evaluates to
a single value
A single value with no
operator is also an
expression
OPERATORS IN PYTHON
The order of operations is
called precedence
The ** operator is
evaluated first the
*, /, //, and % operators
are evaluated
next(from left to right)
and the + and -
operators are evaluated
last (from left to right).
Parentheses to override
the usual precedence if
you need to.
OPERATORS IN PYTHON
THE INTEGER, FLOATING-POINT,
AND STRING DATA TYPES
THE INTEGER, FLOATING-POINT,
AND STRING DATA TYPES
A data type is a category for values
The integer (or int) data type indicates values that are
whole numbers
Numbers with a decimal point are called floating-point
numbers (or float)
Python programs can also have text values enclosed
in single quotes called strings (or str)
A string with no characters is called an empty string or
blank string
THE INTEGER, FLOATING-POINT,
AND STRING DATA TYPES
STRING CONCATENATION AND
REPLICATION
STRING CONCATENATION AND
REPLICATION
The + operator is used for
string concatenation
Both operands must be
strings for concatenation
The * operator is used for
string replication
One of the operands is a
string and the other is an
integer for string
replication
COMMENTS IN PYTHON
COMMENTS IN PYTHON
Comments are needed to make a code readable and for
documentation
Comments in Python are written using '#' symbol
For multi-line comments, '#' is placed at the beginning of
the comment in every line
Multi-line comments are also written within a pair of
three single quotes
COMMENTS IN PYTHON
STORING VALUES IN VARIABLES
STORING VALUES IN VARIABLES
Variables are not declared with a datatype in Python
Python is a dynamically-typed programming language
The name of a variable should obey the following
rules:
It can be only one word with no spaces
It can use only letters, numbers and underscore character
It cannot begin with a number
Variable names are case sensitive
Values in variables are stored with an assignment
statement
STORING VALUES IN VARIABLES
len(), input() and print()
FUNCTIONS
len(), input() and print() FUNCTIONS
The print() function displays the string value inside its
parentheses on the screen
The input() function waits for the user input
The len() function accepts a string value (or a variable
containing a string), and the function evaluates to the
integer value of the number of characters in that
string
len(), input() and print() FUNCTIONS
len(), input() and print() FUNCTIONS
The input() accepts input from user and stores it as a
string
For math operations, the input should be converted to
appropriate data type before evaluation
FIRST PROGRAM
Once you’ve entered the bellow source code, save
it so that you won’t have to retype it each time you
start. Click the Save button, enter sa.py in the File
Name field, and then click Save.
UTPUT
THE str(), int(), and float() FUNCTIONS
THE str(), int(), and float()
FUNCTIONS
The str() function can be passed an integer value and
will evaluate to a string value version of the integer
The int() function can be passed an string or a float
value and will evaluate to an integer value
The float() function can be passed an integer value
and will evaluate to a float value
THE str(), int(), and float() FUNCTIONS
>>> str(0)
'0’
>>> str(-3.14)
'-3.14’
>>> int('42’)
42
>>> int('-99’)
-99
>>> int(1.25)
1
>>> int(1.99)
1
>>> float('3.14’)
3.14
>>> float(10)
10.0
CAN U GUESS
>>> spam = input() >>> 42 == '42'
101 False
>>> spam >>> 42 == 42.0
'101’ True
spam = int(spam) >>> 42.0 ==
>>> spam 0042.000
101 True
>>>spam * 10 / 5
202.0
>>> int('99.99')
CONTD.,
The int() function is also useful if you need to
round a floating-point number down.
>>> int(7.7)
7
>>> int(7.7) + 1
8
CONTD….
END