[go: up one dir, main page]

0% found this document useful (0 votes)
24 views5 pages

PYTHON

Python is a widely used programming language known for its simplicity and readability. It was created in 1991 and emphasizes code readability through its easy-to-learn syntax. Python supports multiple programming paradigms and has a vast standard library, making it suitable for various applications. It has a dynamic typing system and automatic memory management that simplify development. Python code can be executed in interactive or script mode, and its community enhances it with extensive libraries. The document then discusses Python identifiers, variables, data types, operators, expressions, debugging, functions and modules.

Uploaded by

sasankponnuru4
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)
24 views5 pages

PYTHON

Python is a widely used programming language known for its simplicity and readability. It was created in 1991 and emphasizes code readability through its easy-to-learn syntax. Python supports multiple programming paradigms and has a vast standard library, making it suitable for various applications. It has a dynamic typing system and automatic memory management that simplify development. Python code can be executed in interactive or script mode, and its community enhances it with extensive libraries. The document then discusses Python identifiers, variables, data types, operators, expressions, debugging, functions and modules.

Uploaded by

sasankponnuru4
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/ 5

PYTHON

5.0 INTRODUCTION TO PYTHON


Python is a versatile and widely used high-level programming language known for its
simplicity and readability. It was created by Guido van Rossum and first released in 1991.
Python's main points include its easy-to-learn syntax, which emphasizes code readability and
reduces the cost of program maintenance. It supports multiple programming paradigms,
including object-oriented, imperative, and functional programming. Python has a vast
standard library, making it suitable for various applications, from web development to
scientific computing. Its dynamic typing system and automatic memory management
simplify the development process. Python's community-driven ecosystem and extensive
third-party libraries enhance its capabilities, making it a popular choice for both beginners
and experienced developers.

5.1 WORKING WITH PYTHON


To write and run (execute) a Python program, we need to have a Python interpreter installed on our
computer or we can use any online Python interpreter. The interpreter is also called Python shell. A
sample screen of Python interpreter is shown in Figure 3.1. Here, the symbol >>> is called Python
prompt, which indicates that the interpreter is ready to receive instructions. We can type commands
or statements on this prompt for execution.

5.2 EXECUTION MODES


There are two ways to run a program using the Python interpreter: a) Interactive mode b) Script mode.

5.2.1 INTERACTIVE MODE


In the interactive mode, we can type a Python statement on the >>> prompt directly. As soon as we
press enter, the interpreter executes the statement and displays the result(s), as shown in Figure 3.2.
Working in the interactive mode is convenient for testing a single line code for instant execution. But
in the interactive mode, we cannot save the statements for future use, and we have to retype the
statements to run them again.

Figure 3.2: Python Interpreter in Interactive Mode


5.2.2 SCRIPT MODE
In the script mode, we can write a Python program in a file, save it and then use the interpreter to
execute the program from the file. Such program files have a .py extension and they are also known as
scripts. Usually, beginners learn Python in interactive mode, but for programs having more than a few
lines, we should always save the code in files for future use. Python scripts can be created using any
editor. Python has a built-in editor called IDLE which can be used to create programs. After opening
the IDLE, we can click File>New File to create a new file, then write our program on that file and save
it with a desired name. By default, the Python scripts are saved in the Python installation folder.

Figure 3.3: Python Code in Script Mode (prog3-1.py)

5.3 IDENTIFIERS
In programming languages, identifiers are names used to identify a variable, function, or other entities
in a program. The rules for naming an identifier in Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_). This
may be followed by any combination of characters a-z, A-Z, 0-9 or underscore (_). Thus, an identifier
cannot start with a digit.

• It can be of any length. (However, it is preferred to keep it short and meaningful).

• It should not be a keyword or reserved word given in Table 3.1.

• We cannot use special symbols like, @, #, $, %, etc. in identifiers.

5.4 VARIABLES
Variable is an identifier whose value can change. For example variable age can have different values
for different people. Variable names should be unique in a program. Value of a variable can be string
(for example, ‘b’, ‘Global Citizen’), number (for example 10,71,80.52) or any combination of
alphanumeric (alphabets and numbers for example ‘b10’) characters. In Python, we can use an
assignment statement to create new variables and assign specific values to them.

gender = 'M'

message = "Keep Smiling"

price = 987.9

Variables must always be assigned values before they are used in the program, otherwise it will lead
to an error. Wherever a variable name occurs in the program, the interpreter replaces it with the
value of that variable.

5.5 DATA TYPES


Every value belongs to a specific data type in Python. Data type identifies the type of data which a
variable can hold and the operations that can be performed on those data. Figure 3.6 enlists the data
types available in Python.
Figure 3.6: Different Data Types in Python

5.5.1 NUMBER
Number data type stores numerical values only. It is further classified into three different types: int,
float and complex.

Boolean data type (bool) is a subtype of integer. It is a unique data type, consisting of two constants,
True and False. Boolean True value is non-zero. Boolean False is the value zero.

Variables of simple data types like integer, float, Boolean etc. hold single value. But such variables are
not useful to hold multiple data values, for example, names of the months in a year, names of
students in a class, names and numbers in a phone book or the list of artefacts in a museum. For this,
Python provides sequence data types like Strings, Lists, Tuples, and mapping data type Dictionaries.

5.5.2 SEQUENCE
A Python sequence is an ordered collection of items, where each item is indexed by an integer value.
Three types of sequence data types available in Python are Strings, Lists and Tuples. A brief
introduction to these data types is as follows:

1)STRING
String is a group of characters. These characters may be alphabets, digits or special characters
including spaces. String values are enclosed either in single quotation marks (for example ‘Hello’) or
in double quotation marks (for example “Hello”). The quotes are not a part of the string, they are
used to mark the beginning and end of the string for the interpreter. For example,

2) List
List is a sequence of items separated by commas and items are enclosed in square brackets []. Note
that items may be of different date types.

3) TUPLE
Tuple is a sequence of items separated by commas and items are enclosed in parenthesis (). This is
unlike a list, where values are enclosed in brackets []. Once created, we cannot change items in the
tuple. Like List, items may be of different data types.

3.5.3 MAPPING
Mapping is an unordered data type in Python. Currently, there is only one standard mapping data type
in Python called Dictionary.
5.6 OPERATOR
An operator is used to perform specific mathematical or logical operations on values. The values that
the operator works on are called operands. For example, in the expression 10 + num, the value 10, and
the variable num are operands and the + (plus) sign is an operator. Python supports several kinds of
operators their categorization is briefly explained in this section.

5.6.1 ARITHMETIC OPERATORS


Python supports arithmetic operators to perform the four basic arithmetic operations as well as
modular division, floor division and exponentiation. '+' operator can also be used to concatenate two
strings on either side of the operator. Some of the Arithmetic operators are Addition, Subtraction,
Multiplication, Division, Modulus, Floor Division, Exponent

5.6.2 RELATIONAL OPERATORS


Relational operator compares the values of the operands on either side and determines the
relationship among them. There are 3 relational operators that are Not equal to (! =), Greater than (>),
Less than (<)

5.6.3 ASSIGNMENT OPERATORS


Assignment operator assigns or changes the value of the variable on its left, Some of the assignment
operators are (=, +=, -=,*=, /=, %=, //=, **=)

5.6.4 LOGICAL OPERATORS


There are three logical operators supported by Python. These operators (and, or not) are to be written
in lower case only. The logical operator evaluates either True or False based on the logical operands
on either side. There are 3 logical operators are Logical AND (and), Logical OR (or), Logical NOT (not)

5.6.5 MEMBERSHIP OPERATORS


Membership operators is used to check if a value is a member of the given sequence or not, there are
2 membership operators.

1) in-Returns True if the variable or value is found in the specified sequence and False otherwise
2) not-in-Returns True if the variable/value is not found in the sequence and False otherwise

5.7 EXPRESSIONS
An expression is defined as a combination of constants, variables, and operators. An expression always
evaluates to a value. A value or a standalone variable is also considered as an expression, but a
standalone operator is not an expression. Some examples of valid expressions are given below.

(i) num – 20.4 (iii) 23/3 -5 * 7(14 -2) (ii) 3.0 + 3.14 (iv) "Global “+” Citizen"

5.8 DEBUGGING
Due to errors, a program may not execute or may generate wrong output:

i) Syntax errors ii) Logical errors iii) Runtime errors


5.9 FUNCTIONS
A function refers to a set of statements or instructions grouped under a name that performs specified
tasks. For repeated or routine tasks, we define a function. A function is defined once and can be reused
at multiple places in a program by simply writing the function name, i.e., by calling that function.

Python has many predefined functions called built-in functions. We have already used two built-in
functions print () and input(). A module is a python file in which multiple functions are grouped
together. These functions can be easily used in a Python program by importing the module using
import command. To use a built-in function we must know the following about that function:

• Function Name — name of the function.


• Arguments — While calling a function, we may pass value(s), called argument, enclosed in
parenthesis, to the function. The function works based on these values. A function may or may not
have argument(s).

• Return Value − A function may or may not return one or more values. A function performs
operations based on argument (s) passed to it and the result is passed back to the calling point.
Some functions do not return any value.

5.10 if..else STATEMENTS


Usually, statements in a program are executed one after another. However, there are situations when
we have more than one option to choose from, based on the outcome of certain conditions. This can
be done using if.. else conditional statements. Conditional statements let us write programs to do
different tasks or take different paths based on the outcome of the conditions. There are three ways
to write if. Else statements:

• if statement — executes the statement(s) inside if when the condition is true.


• if...else statement executes the statement(s) inside if when the condition is true, otherwise executes
the statement(s) inside else (when the condition is false

• if...elif....else is use dot check multiple conditions and execute statements accordingly. Meaning of
elif is elseif. We can also write elseif instead of elif for more clarity.

5.10 FOR LOOP


Sometimes we need to repeat certain things for a particular number of times. For example, a program
must display attendance for every student of a class. Here the program must execute the print
statement for every student. In programming, this kind of repetition is called looping or iteration, and
it is done using for statement. For statement is used to iterate over a range of values or a sequence.
The loop is executed for each item in the range. The values can be numeric, string, list, or tuple. When
all the items in the range are exhausted, the statements within loop are not executed and Python
interpreter starts executing the statements immediately following the four loop. When using for loop,
we should know in advance the number of times the loop will execute.

5.11 THE range () FUNCTION


The range () is a built-in function in Python. Syntax of range () function is: range([start], stop[, step])

You might also like