INTRODUCTION TO PYTHON
What is program
A computer program is a collection of instructions that perform a specific task
when executed by a computer. The purpose of programs is to make computer
programs run faster, safer, and more efficiently. Programs do everything in a
computer: they read and write data, manage memory, and perform
calculations
Applications of Python
There are different type of Python Application –
1. Web and Internet
Development
2. Desktop GUI Application
3. Software Development
4. Database Access
5. Business Application
6. Games and 3D Graphics
Python IDLE installation
After installing Python, you’ll need an IDE to write Python programmes. IDLE
is a Python editor with a graphical user interface. IDLE stands for Integrated
Development Environment. This IDLE is also known as the Python shell, and
it has two modes of operation: interactive mode and script mode.
Interactive Mode
Python IDLE Shell provides a Python prompt, You can write single line python
commands and execute them easily.
Script Mode
In Python, the Script Mode allows you to add numerous lines of code. In script
mode, we type a Python programme into a file and then use the interpreter to
run the code.
Python Statement
A statement is a piece of code that can be executed by a Python interpreter.
So, in simple words, anything written in Python is a statement.
Type of Multi-line Statement Usage
Using Continuation Character (/) s=1+2+3+\
4+5+6+\
7+8+9
Using Parentheses () n = (1 * 2 * 3 + 4 – 5)
footballer = [‘MESSI’,
Using Square Brackets [] ‘NEYMAR’,
‘SUAREZ’]
x = {1 + 2 + 3 + 4 + 5 + 6 +
Using braces {}
7 + 8 + 9}
Using Semicolons ( ; ) flag = 2; ropes = 3; pole = 4
Python Comments
In Python, comments are lines of code that are skipped by the interpreter
while the programme is being run.
a. Single Line comment
A single-line comment in Python begins with the hash symbol (#) and
continues until the end of the line.
Example
# Single line comment
b. Multiple Line comment
There are a variety of methods for writing multiline comments.
Example
c. Using Multiple hash (#)
# Multiple line comment 1
# Multiple line comment 2
d. Multiline comment using String literals
“ “ “ Multiline comment in
Python statement “ “ “
Or
‘ ‘ ‘ Multiline comment in
Python statement ‘ ‘ ‘
Python Keywords and Identifiers
Keywords – Keywords are reserved words in Python that the Python
interpreter uses to recognise the program’s structure. In Python, keywords are
predefined words with specific meanings. The keyword can’t be used as a
variable name, function name, or identifier. Except for True and False, all
keywords in Python are written in lower case.
Example of Keywords –
False, class, finally, is, return, None, continue, for lambda, try, True, def, from,
nonlocal, while, and, del, global, not, with, as, elif, if, or, yield, assert, else,
import, pass, break, except, in, raise etc.
Identifiers – An identifier is a name given to a variable, function, class,
module, or other object. The identification is made up of a series of digits and
underscores. The identification should begin with a letter or an Underscore
and then be followed by a digit. A-Z or a-z, an UnderScore (_), and a numeral
are the characters (0-9). Special characters (#, @, $, percent,!) should not be
used in identifiers.
1. Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore _.
2. An identifier cannot start with a digit
3. Keywords cannot be used as identifiers
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier
5. Identifier can be of any length
6. Python is a case-sensitive language.
Example of Identifier
var1
_var1
_1_var
var_1
Variables
A variable is a memory location where you store a value in a programming
language. In Python, a variable is formed when a value is assigned to it
Constants
A constant is a kind of variable that has a fixed value. Constants are like
containers that carry information that cannot be modified later.
Declaring and assigning value to a constant
NAME = “Rajesh Kumar”
AGE = 20
Datatype
In Python, each value has a datatype. Data types are basically classes, and
variables are instances (objects) of these classes, because everything in
Python programming is an object.
Python has a number of different data types. The following are some of the
important datatypes.
1. Number
2. Sequences
3. Sets
4. Maps
Operators
Operators are symbolic representations of computation. They are used with
operands, which can be either values or variables. On different data types, the
same operators can act differently. When operators are used on operands,
they generate an expression.
Operators are categorized as –
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Type Conversion
Type conversion is the process of converting the value of one data type
(integer, text, float, etc.) to another data type. There are two types of type
conversion in Python.
1. Implicit Type Conversion
2. Explicit Type Conversion
Implicit Type Conversion
Python automatically changes one data type to another via implicit type
conversion. There is no need for users to participate in this process.
Explicit Type Conversion
Users transform the data type of an object to the required data type using
Explicit Type Conversion.
To do explicit type conversion, we employ predefined functions such as int(),
float(), str(), and so on.