Introduction To Python
Introduction To Python
• Python is a Beginner's Language: Python is a great language for the beginner level
programmers and supports the development of a wide range of applications from simple
Python was developed by Guido van Rossum in the late 80s and early 90s at the
National Research Institute for Mathematics and Computer Science in the Netherlands.
Python Features
Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows a
student to pick up the language quickly.
Easy-to-read: Python code is more clearly defined and visible to the eyes.
A broad standard library: Python's bulk of the library is very portable and cross platform compatible
on UNIX, Windows, and Macintosh.
Interactive Mode: Python has support for an interactive mode, which allows interactive testing and
debugging of code.
Portable: Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
GUI Programming: Python supports GUI applications that can be created and ported to many system
calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of
Unix.
Python Official Website :
http://www.python.org/ Getting Python
Binaries of latest version of Python 3 (Python Linux platform
3.5.1) are available on this download page Different flavors of Linux use different package managers
for installation of new packages. On Ubuntu Linux, Python 3
The following different installation options are is installed using the following command from the terminal.
available.
• Windows x86-64 embeddable zip file $sudo apt-get install python3-minimal
• Windows x86-64 executable installer
Download Gzipped source tarball from Python's
• Windows x86-64 web-based installer download URL:
https://www.python.org/ftp/python/3.5.1/Python-
3.5.1.tgz
Mac OS Extract the tarball
Download Mac OS installers from this tar xvfz Python-3.5.1.tgz
URL:https://www.python.org/downloads/m Configure and Install:
ac-osx/ cd Python-3.5.1
Mac OS X 64-bit/32-bit installer : python- ./configure --prefix=/opt/python3.5.1
3.5.1-macosx10.6.pkg make
Mac OS X 32-bit i386/PPC installer : sudo make install
python-3.5.1-macosx10.5.pkg
Script Mode Programming & Run
Let us write a simple Python program in a script.
Python files have the extension .py.
print (“Hello, Python!”)
On Linux
nano rasp_1.py #to create python file
Python rasp_1.py #to get output
On Windows
C:\Python34>Python test.py
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
a@ = 0
File "<interactive input>", line 1
a@ = 0
SyntaxError: invalid syntax
Quotation in Python
Python accepts single ('), double (") and triple (''' or """) quotes to denote
string literals, as long as the same type of quote starts and ends the string.
Word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made up of multiple lines and sentences."""
Comments in Python
A hash sign (#) that is not inside a string literal is the beginning of a comment. All
characters after the #, up to the end of the physical line, are part of the comment and the
Python interpreter ignores them.
# First comment
print ("Hello, Python!") # second comment
• print (C[2])
• print (D[1])
• print (S[1:4])
Python Input, Output
Some of the functions like input() and print() are widely used for standard input and
output operations respectively.
Example:
a = 5
print('The value of a is', a)
# Output: The value of a is 5
print(1,2,3,4) # Output: 1 2 3 4
print(1,2,3,4,sep='*') # Output: 1*2*3*4
print(1,2,3,4,sep='#',end='&') # Output: 1#2#3#4&
Output formatting in string format
Sometimes we would like to format our output to make it look attractive. This can be done by
using the str.format() method. This method is visible to any string object.
x = 5; y = 10
print('The value of x is {} and y is {}'.format(x,y))
Type of operators in
Python
Arithmetic operators
Comparison (Relational) operators
Logical (Boolean) operators
Arithmetic operators
x = 15
y = 4
print('x + y =',x+y) # Output: x + y = 19
print('x ** y =',x**y)
# Output: x ** y = 50625
Comparison operators
It either returns True or False according to the condition.
x = 10
y = 12
print('x > y is',x>y)
# Output: x > y is False
print('x < y is',x<y)
# Output: x < y is True
print('x == y is',x==y)
# Output: x == y is False
print('x != y is',x!=y)
# Output: x != y is True
print('x >= y is',x>=y)
# Output: x >= y is False
print('x <= y is',x<=y)
# Output: x <= y is True
Logical operators
Logical operators are the and , or , not operators.
x = True
y = False
>>>print(a+b)
3
Python Statement
Decision making is required when we want to execute a code only if a certain condition is satisfied.
• The if…elif…else statement is used in Python for decision making.
if test expression:
Body of if
else:
Body of else
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Python if...elif...else Syntax
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
4
6
8
for loop with else
A for loop can have an optional else block as well. The else part is executed if the items in the
sequence used in for loop exhausts. break statement can be used to stop a for loop. In such case,
the else part is ignored.
Hence, a for loop's else part runs if no break occurs.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
# Output :
0
1
5
No items left.
Python while Loop
The while loop in Python is used to iterate over a block of code as long as the test expression
(condition) is true.
Syntax of while Loop in Python
# Output : 55
Python break statement
The working of break statement in for loop and while loop is shown
below.
while m>f:
print("m bigger")
break
while m==f:
print(“twins")
break
while m<f:
print("m smaller")
break
Ex-3Write a program to input a number and
determine whether it is an odd number or
even number.
Happy ! Coding...