PYTHON
Computer programming is the process of
writing, testing, troubleshooting, debugging and
maintaining of a computer program.
In order to tell the computer “what you want to
do”, we write a program in a language which
computer can understand. Though there are
many different programming languages such
as BASIC, Pascal, C, C++, Java , Ruby,
Python, etc.
We will study Python in this course. Before
learning the technicalities of Python, let’s get
familiar with it.
Introduction
FEATURES OF PYTHON
a) Python can be used on a server to create
web applications.
b) Python can connect to database systems.
It can also read and modify files.
c) Python can be used to handle big data
and perform complex mathematics.
d) Python can be used for rapid prototyping,
or for production-ready software
development.
WHO USES PYTHON ??
PYTHON IS EASY TO READ,
WRITE & LEARN
PYTHON IS EASY TO READ,
WRITE & LEARN
FEATURES OF PYTHON
a) Easy to Learn and Use
b) Expressive Language
c) Interpreted Language
d) Cross-platform Language
e) Free and Open Source
f) Object-Oriented Language
TWO MODES TO USE THE
PYTHON INTERPRETER
a) Interactive Mode b) Script Mode
a) Interactive Mode: Without passing python
script file to the interpreter, directly execute
code to Python (Command line).
Example :Print sum of two numbers in
Interactive Mode
b) Script Mode: In script mode, we type
Python program in a file and then use the
interpreter to execute the content from the
file.
Example : Program to add two numbers
When the above code is executed, it produces
the following result –
To create and run a Python script, we will use
following steps in IDLE, if the script mode is not
made available by default with IDLE
environment.
• File>New File (for creating a new script file)
• Write the Python code in script mode
• Give the file name and Save it (CTRL+S)
• Execute it in interactive mode- by using Run-
>Run Module option or F5.
PYTHON CHARACTER SET:
• A character set defines the valid characters
that can be used in source programs.
• It is a set of valid characters that a
language recognizes.
• Letters: A-Z, a-z
• Digits : 0-9
• Special Characters : ! # % ^ & * ( ) - _ = +
~ ' " : ; ? / | \ { } ,[ ] , . <> $
• Whitespace Characters
Keywords : Keywords are the reserved
words in Python. We cannot use a
keyword as a variable name, function
name or any other identifier.
Identifiers :Identifiers are the names of variables,
functions, classes, lists and dictionaries.
Rules For Defining Identifiers In Python:-
[Link] identifiers can start with a character,
underscore (_) but not digit.
2. After the first character, an identifier can have any
combination of characters.
3. No special character except underscore ( _) can
be used as an identifier.
4. Python keywords cannot used as Identifiers
5. Identifiers in Python are case sensitive; apple and
Apple are two different identifiers.
Operators
Operators are special symbols which represents
computation. They are applied on operand(s), which
can be values or variables.
If x=17 and y=3
Variables :Variables are containers for storing data
values.
Creating a variable:
A variable is created the moment you first assign a
value to it.
Example:
x = 10
y = “Welcome”
Variables do not need to be declared with any
particular type and can even change type after they
have been set. It is known as dynamic Typing.
v = 8 # v is of type int
v = "python" # v is now of type string
print(v)
*Python allows assign a single value to multiple
variables.
For example: a = b = c = 6
You can also assign multiple values to multiple
variables.
For example : a , b , c = 3, 7, “python” 3 is assigned
to a, 7 is assigned to b and string “python”
assigned to variable c respectively.
input() METHOD AND print() METHOD IN
PYTHON
i) print( ) :The print() function prints the specified
message to the screen, or other standard output
device.
print(“Welcome to Python Programming”)
This function prints “Welcome to Python
Programming”
input() METHOD AND print() METHOD IN PYTHON
ii) input( ): The input( ) method is used to take input
from the user.
This program takes the input name from the user
and print along with “Hello”.
Output:
PYTHON COMMENTS
• Comments can be used to explain Python code.
• Comments can be used to make the code more
readable.
• Comments can be used to prevent execution
when testing code.
a) Single line comment
⮚Comments starts with a # and Python will ignore
them:
Example
#This is a comment
print("Hello, World!")
⮚ Comments can be placed at the end of a line, and
Python will ignore the rest of the line:
Example
print("Hello, World!") #This is a comment
⮚ Comments does not have to be a text to explain
the code, it can also be used to prevent Python
from executing code:
Example
#print("Hello, World!")
print(“Welcome, Mate!")
b) Multi Line Comments: Python does not really
have syntax for multi line comments. To add a
multiline comment you could insert a # for each
line.
Example
#This is a comment
#written in
#more than just one line
print("Hello, World!") or
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")