Chapter-3
Chapter-3
What is Python?
• A Python program is a script, which is vaguely a short program that
can be executed in Python interactive script.
• A statement is a line of code or command or instruction for
computers to perform initialize a number, perform some
computations, and do some control flow.
• A Function is a collection of statements that perform an intended
action and returns a Value.
• There are two types of statements: Simple and Compound.
Simple Statement: Performs a single simple task. For Ex., Expressions
and Assignment Statements.
• Compound Statements: This is a group of statements that spans multiple
lines and that control the execution of other statements of that program.
• For example, if, while and function are examples of the compound
statement.
• Example: Illustration of swapping of variables in Interactive mode
>>> x = 10
>>> y = 20
>>> t = x
>>> x = y
>>> y = t
>>> x, y
(20, 10)
>>>
Differences Between Python 2.x and 3.x
No Python 2.x Python 3.x
2. The syntax of the print statement is print The syntax of Python 3. x print is changed.
"hello" Print is a function and it should be
print("Hello").
5. The division is awkward in Python 2.x. The division of 7 by 2 results in 3.5; hence,
Division of 7/2 gives 3. calculations are more predictable.
Elements of Python Language
• The set of valid characters that a programming language can recognize is called the character set of the
language. Two types of character sets are
• Source Characters. ( Alphabets uppercase and lowercase, underscore, Special characters blank –(+,-
,*,/,^,~,%,!,&,|,(),{},[] ?’, ;, \), blank(“”))
• The Token is the smallest lexical unit in a program. The Types of tokens are listed below.
1. Identifiers
2. Keywords
3. Literals
4. Punctuations
Identifiers
• One can also accept a string using the input() function. The syntax is
given as
Illustration of Input Illustration of Type Error Illustration of input Illustration of eval
statement statement
• Illustration of Print
Formatting using Print Statement
Many times a simple printing of values is not sufficient. Many projects may require fancy printing as per the requirements. It
is called formatting. All variables and constants need to be converted to string type for printing purposes.
• Built-in Methods
String Formatting
• String formatting is the second way of formatting; here, there will be a format string and arguments.
• The format of this string formatting is given as
• Formatting Illustration
Built-in functions