0% found this document useful (0 votes)
16 views

Ch1.pptx

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Ch1.pptx

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Dr.

Tulika
CHAPTER 1 Assistant Professor
PYTHON PROGRAMMING: AN Department of Computer Science
INTRODUCTION Miranda House
REF. BOOK- TANEJA, S. & KUMAR, N., “PYTHON
PROGRAMMING- A MODULAR APPROACH”,
PEARSON EDUCATION
INDIA, 2018.
PROGRAMMING LANGUAGE
A programming language is a set of instructions written by a programmer to deliver instructions to the
computer to perform and accomplish a task.
Examples are: Python, Ruby, Java, C , C++, C# etc.
Some of the key features of programming languages include:
Syntax: The specific rules and structure used to write code in a programming language.
Data Types: The type of values that can be stored in a program, such as numbers, strings, and booleans.
Variables: Named memory locations that can store values.
Operators: Symbols used to perform operations on values, such as addition, subtraction, and comparison.
Control Structures: Statements used to control the flow of a program, such as if-else statements, loops, and
function calls.
Libraries and Frameworks: Collections of pre-written code that can be used to perform common tasks and
speed up development.
WHAT IS PYTHON?
Python is an interactive programming language.
Simple syntax of the language makes Python programs easy to read and write.
Python was developed by Guido Van Rossum in 1991 at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
Python is used in various application areas such as the web, gaming, scientific and numeric
computing, text processing, and network programming.
LANGUAGE FEATURES
Interpreted: There are no separate compilation and execution steps like C and C++. Directly
run the program from the source code. No need to worry about linking and loading with
libraries, etc. In an interpreted language, the code is executed line by line, while in a
compiled language, the entire code is converted into machine language before execution.
Platform Independent: Python programs can be developed and executed on multiple
operating system platforms. Python can be used on Linux, Windows, and many more.
Free and Open Source
High-level Language: In Python, no need to take care about low-level details such as
managing the memory used by the program.
Simple: Closer to English language; Easy to Learn
More emphasis on the solution to the problem rather than the syntax
1.1 IDLE – AN INTERPRETER FOR
PYTHON
IDLE stands for Integrated Development and Learning Environment.
Python IDLE comprises Python shell and Python Editor. While Python shell is an interactive
interpreter, Python Editor allows us to work in script mode.
While using Python shell, we just need to type Python code at the >>> prompt and press the
enter key, and the shell will respond with the result of the computation.
Python shell may also be used as a calculator, for example, when we type 18 + 5 followed by
enter, Python shell outputs the value of the expression, i.e. 23, as:
>>> 18 + 5
23
operator + acts on the operands 18 and 5. In the expression 18 + 5, + is called the operator.
The numbers, 18 and 5 on which the operator + is applied, are called the operands.
In general, an expression is a valid combination of operators and operands.
In Python, all forms of data are called values or objects. For example, 18, 5, and 23 are
objects.
Note: The result of division
27 / 5 is real number.
In Python, result of division is always a
real number.
However, if you wish to obtain an
integer result (for example, floor(5.4)),
Python operator // (two slashes without
any intervening blank) can be used.
Python operator % (percentage sign),
known as modulus, yields the remainder
of the division.

The exponentiation operator (**) yields


raise to the power operation, i.e. ab . For
example, 3 is expressed as 3 ** 2, and
yields the value 9.
The operators /, +, -, *, //, % are evaluated left to right, the exponentiation operator is
evaluated right to left.
We say that the operators, +, -, *, /, //, and %, are left associative operators and the operator
** is right associative.
1.2 PYTHON STRINGS
A string is a sequence of characters. To specify a string, we may enclose a sequence of
characters between single, double, or triple quotes.
Thus, 'Hello world', ' "Ajay " ', "what's ", '''today's "action" plan?''' are the examples of
strings.
A string enclosed in single quotes may include double quote marks and vice versa.
A string enclosed in triple quotes (also known as docstring, i.e. documentation string) may
include both single and double quote marks and may extend over several lines, for example:
The concatenation operator (+), which is used to produce a string by concatenating strings;
for example, the expression 'hello ' + '!!' yields the string 'hello !!’.
The operator * (multiplication) is used to repeat a string a specified number of times; for
example, the expression 'hello’ * 5 yields the string 'hellohellohellohellohello’.
1.3 RELATIONAL OPERATORS
Relational operators are used for comparing two expressions and yield True or False. In an
expression involving both arithmetic and relational operators, the arithmetic operators have
higher precedence than the relational operators.
Thus, the expressions, 23 < 25, 23 != 23, and 23 - 2.5 >= 5 *4, yield True, False, and True,
respectively.
As arithmetic operators have higher precedence than the relational operators, the expression
23 - 2.5 >= 5 * 4 is evaluated as if it were (23 -2.5) >= (5 * 4).
When the relational operators are applied to strings, strings are compared left to right,
character by character, based on their ASCII codes, also called ASCII values.
For example, ASCII codes of 'A'-'Z', 'a'-'z', and '0'-'9' lie in the range [65, 90], [97, 122], and
[48, 57], respectively. Thus, 'h' > 'H' yields True as ASCII value of 'h' (= 104) is greater than
ASCII value of 'H' (= 72). Also, 'hello' > 'Hello' yields True since 'h' is greater than 'H’.
Also, if a string is a prefix of another string, the longer string will be considered larger.
Relational operators yield values: True, False
ASCII values of characters are used for string comparison
Python 3 does not allow string values to be compared with numeric values
1.4 LOGICAL OPERATORS
The logical operators not, and, and or are applied to logical operands True and False, also
called Boolean values, and yield either True or False.
The operator not is a unary operator, i.e. it requires only one operand. The expressions not
True and not False yield False and True, respectively.
An expression involving two Boolean operands and the and operator yields True if both the
operands are True, and False otherwise. Similarly, an expression involving two Boolean
operands and the or operator yields True if at least one operand is True, and False otherwise.
While evaluating an expression involving an and operator, the second sub-expression is
evaluated only if the first subexpression yields True.
For example, in the expression (10 < 5) and ((5 / 0) < 10), since the first sub-expression (10 <
5) yields False, and thus determines the value of the entire expression as False, Python does
not evaluate the second subexpression. This is called short-circuit evaluation of Boolean
expression.
However, the expression (10 > 5) and ((5 / 0) <10) yields an error since the first
sub-expression yields True, and Python attempts to evaluate the second sub-expression ((5 /
0) < 10) that yields an error because division by zero is an illegal operation.
To evaluate an expression comprising arithmetic, relational, and logical operators, the
operators are applied according to precedence order for the operators.
1.6 VARIABLES AND ASSIGNMENT
STATEMENTS
Variables provide a means to name values so that they can be used and manipulated later on.
If a student has obtained 57 marks in english, this may be expressed in Python using the
following statement:
>> english = 57
a variable is a name given to a value
an assignment statement binds a variable to an object
syntax for assignment statement is as follows:
variable = expression
While forming variables, it is important to note that Python is case-sensitive. Thus, age and
Age are different variables.
>>> a += 5
a shorthand notation
Thus, the operator += serves as shorthand notation in assignment statement.
>>> totalMarks = count = 0
assigning same value to multiple variables in a single statement may be used to
replace the following two assignments:
>>> totalMarks = 0
>>> count = 0
we can use the following statement to swap the values of two variables:
>>> num1, num2 = num2, num1
1.7 KEYWORDS
Keywords are the reserved words that are already defined by the Python for specific
uses. They cannot be used for any other purpose. Hence, make sure that none of the
names used in a program should match any of the keywords.
1.8 SCRIPT MODE
The definitions of objects, names, etc., exist only during an IDLE session. When we exit an
IDLE session, and start another IDLE session, we must redo all computations. This is not a
convenient mode of operation for most of the computational tasks.
Python provides another way of working called script mode. All the instructions necessary for
a task are stored in a file often called a source file script, program, source code, or a module.
Python requires that such a file must have extension .py or .pyw.
To create a script, Python IDLE provides a New Window option in the File menu. On
clicking the New Window option, a Python Editor window opens, where we can write a
sequence of instructions, collectively called a program or a script.

You might also like