Ankit PYTHON LAB
Ankit PYTHON LAB
PYTHON(CS-506)
LAB MANUAL
Ex.no.1
Introduction to Python Programming.
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
Why Python?
2
Name:Ankit Verma Enrollment No: 0805CS201016
Good to know
3
Name:Ankit Verma Enrollment No: 0805CS201016
Python Getting Started
Python Install
Many PCs and Macs will have python already installed.
python --version
If you find that you do not have python installed on your computer,
then you can download it for free from the following
website: https://www.python.org/
4
Name:Ankit Verma Enrollment No: 0805CS201016
Python Quickstart
Python is an interpreted programming language, this means that as a
developer you write Python (.py) files in a text editor and then put those
files into the python interpreter to be executed.
The way to run a python file is like this on the command line:
Let's write our first Python file, called helloworld.py, which can be done
in any text editor.
helloworld.py
print("Hello, World!")
Simple as that. Save your file. Open your command line, navigate to the
directory where you saved your file, and run:
Hello, World!
5
Name:Ankit Verma Enrollment No: 0805CS201016
Ex.no.2
Python Syntax
As we learned in the previous page, Python syntax can be executed by
writing directly in the Command Line:
Or by creating a python file on the server, using the .py file extension,
and running it in the Command Line:
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
6
Name:Ankit Verma Enrollment No: 0805CS201016
Example:
if 5 > 2:
if 5 > 2:
output:
File "demo_indentation_test.py", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block
if 5 > 2:
if 5 > 2:
output:
Five is greater than two!
Five is greater than two!
7
Name:Ankit Verma Enrollment No: 0805CS201016
Ex.no.3
Python Comments
Comments can be used to explain Python code.
Creating a Comment
Comments starts with a #, and Python will ignore them
#This is a comment.
print("Hello, World!") …
Comments does not have to be text to explain the code, it can also be used to
prevent Python from executing code:
#print("Hello, World!")
print("Cheers, Mate!")
#This is a comment
#written in
8
Name:Ankit Verma Enrollment No: 0805CS201016
print("Hello, World!")
Ex.no.4
Python Variables
Creating Variables
Variables are containers for storing data values.
x=5
y = "John"
print(x)
print(y)
OUTPUT: 5,John
Variables do not need to be declared with any particular type and can even
change type after they have been set.
x=4
x = "Sally"
print(x)
9
Name:Ankit Verma Enrollment No: 0805CS201016
OUTPUT: Sally
x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)
OUTPUT: John John
10
Name:Ankit Verma Enrollment No: 0805CS201016
Ex.no.5
Python Data Types
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
Example
Print the data type of the variable x:
11
Name:Ankit Verma Enrollment No: 0805CS201016
x=5
print(type(x)), OUTPUT: <class 'int'>
x = 20 int
x = 20.5 float
x = 1j complex
x = range(6) range
12
Name:Ankit Verma Enrollment No: 0805CS201016
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
13
Name:Ankit Verma Enrollment No: 0805CS201016
Ex.no.6
Python Numbers:
Python Numbers
There are three numeric types in Python:
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example-
x = 1 # int
y = 2.8 # float
z = 1j # complex
x = 1
y = 2.8
z = 1j
print(type(x))
print(type(y))
print(type(z))
OUTPUT:
14
Name:Ankit Verma Enrollment No: 0805CS201016
<class 'int'>
<class 'float'>
<class 'complex'
Ex.no.7
Python Casting:
Specify a Variable Type
Example
Integers:
x = int(1)
y = int(2.8)
z = int("3")
print(x)
15
Name:Ankit Verma Enrollment No: 0805CS201016
print(y)
print(z)
OUTPUT- 1 2 3
Example
Floats:
x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
print(x)
print(y)
print(z)
print(w)
OUTPUT-
1.0
2.8
3.0
4.
Ex.no.8
16
Name:Ankit Verma Enrollment No: 0805CS201016
Python Strings:
String Literals
String literals in python are surrounded by either single quotation marks, or
double quotation marks.
print("Hello")
print('Hello')
OUTPUT-
Hello
Hello
17