0% found this document useful (0 votes)
6 views

Python Practical

The document discusses Python programming including its features, data types, operators, and some example programs. It covers numeric, string, sequence, and dictionary data types as well as boolean values and provides examples of arithmetic operations, finding triangle properties, appending lists, and sorting dictionaries.

Uploaded by

Bagwan Shoaib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Practical

The document discusses Python programming including its features, data types, operators, and some example programs. It covers numeric, string, sequence, and dictionary data types as well as boolean values and provides examples of arithmetic operations, finding triangle properties, appending lists, and sorting dictionaries.

Uploaded by

Bagwan Shoaib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Aim: Study of Python Programming

Features of Python Programming:


1. High-level Language
2. Easy to Code
3. Easy to Read
4. Free and Open-Source
5. Robust Standard Library
6. Interpreted
7. Portable
8. Object-Oriented and Procedure-Oriented
9. Extensible
10. Expressive
11. Support for GUI
12. Dynamically Typed
13. Simplify Complex Software Development

Python contains several advanced programming features such as generators (used to create
iterators with a different approach that most other languages) and list comprehensions (used to
create new lists from other iterables). Python also has automatic memory management eliminating
the need to manually allocate and free memory in the code.
Data types in Python:

Python Data Types are used to define the type of a variable. It defines what type of data we are
going to store in a variable. The data stored in memory can be of many types. For example, a
person's age is stored as a numeric value and his or her address is stored as alphanumeric
characters.
Python has following built-in data types:

A) Numeric (Numbers) - int, float, complex

Python numeric data types store numeric values. Number objects are created when you
assign a value to them.
Python supports four different numerical types −
 int (signed integers)
 long (long integers, they can also be represented in octal and hexadecimal)
 float (floating point real values)
 complex (complex numbers)

B) String –str
Python Strings are identified as a contiguous set of characters represented in the quotation
marks. Python allows for either pairs of single or double quotes. Subsets of strings can be
taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the
string and working their way from -1 at the end.The plus (+) sign is the string
concatenation operator and the asterisk (*) is the repetition operator in Python.
str='Hello World!'

print(str)# Prints complete string


print(str[0])# Prints first character of the string
print(str[2:5])# Prints characters starting from 3rd to 5th
print(str[2:])# Prints string starting from 3rd character
print(str*2)# Prints string two times
print(str+"TEST")# Prints concatenated string
This will produce the following result –
ello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

C) Sequence - list, tuple, range

List: Python Lists are the most versatile compound data types. A Python list contains items
separated by commas and enclosed within square brackets ([]). To some extent, Python
lists are similar to arrays in C. One difference between them is that all the items belonging
to a Python list can be of different data type where as C array can store elements related to
a particular data type.
The values stored in a Python list can be accessed using the slice operator ([ ] and [:]) with
indexes starting at 0 in the beginning of the list and working their way to end -1. The plus
(+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator. For
example −

Binary - bytes, byte array, memory view

list=['abcd',786,2.23,'john',70.2]
tinylist=[123,'john']

print(list)# Prints complete list


print(list[0])# Prints first element of the list
print(list[1:3])# Prints elements starting from 2nd till 3rd
print(list[2:])# Prints elements starting from 3rd element
print(tinylist*2)# Prints list two times
print(list+tinylist)# Prints concatenated lists
This produce the following result –

['abcd', 786, 2.23, 'john', 70.2]


abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Python Tuple Data Type
Python tuple is another sequence data type that is similar to a list. A Python tuple consists of a
number of values separated by commas. Unlike lists, however, tuples are enclosed within
parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their
elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated. Tuples can be thought of as read-only lists.
For example −
tuple=('abcd',786,2.23,'john',70.2)
tinytuple=(123,'john')

print(tuple)# Prints the complete tuple


print(tuple[0])# Prints first element of the tuple
print(tuple[1:3])# Prints elements of the tuple starting from 2nd till 3rd
print(tuple[2:])# Prints elements of the tuple starting from 3rd element
print(tinytuple*2)# Prints the contents of the tuple twice
print(tuple+tinytuple)# Prints concatenated tuples

This produce the following result –

('abcd', 786, 2.23, 'john', 70.2)


abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
Python Ranges
Python range() is an in-built function in Python which returns a sequence of numbers starting
from 0 and increments to 1 until it reaches a specified number.
We use range() function with for and while loop to generate a sequence of numbers. Following is
the syntax of the function:
range(start, stop, step)

 start: Integer number to specify starting position, (Its optional, Default: 0)


 stop: Integer number to specify starting position (It's mandatory)
 step: Integer number to specify increment, (Its optional, Default: 1)

Python Dictionary
Python dictionaries are kind of hash table type. They work like associative arrays or hashes found
in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are
usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using
square braces ([]).
For example −
dict={}
dict['one']="This is one"
dict[2]="This is two"

tinydict={'name':'john','code':6734,'dept':'sales'}

print(dict['one'])# Prints value for 'one' key


print(dict[2])# Prints value for 2 key
print(tinydict)# Prints complete dictionary
print(tinydict.keys())# Prints all the keys
print(tinydict.values())# Prints all the values

This produce the following result –


This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']

Python Boolean Data Types


Python boolean type is one of built-in data types which represents one of the two values
either True or False. Python bool() function allows you to evaluate the value of any expression
and returns either True or False based on the expression.
Program 1 :Write a Program to Perform Arithmetic Operations
num1 = float(input(" Please Enter the First Value Number 1: "))
num2 = float(input(" Please Enter the Second Value Number 2: "))
# Add Two Numbers
add = num1 + num2
# Subtracting num2 from num1
sub = num1 - num2
# Multiply num1 with num2
multi = num1 * num2
# Divide num1 by num2
div = num1 / num2
print("The Sum of {0} and {1} = {2}".format(num1, num2, add))
print("The Subtraction of {0} from {1} = {2}".format(num2, num1, sub))
print("The Multiplication of {0} and {1} = {2}".format(num1, num2, multi))
print("The Division of {0} and {1} = {2}".format(num1, num2, div))

Program 2 : Write a Program to find area and Perimeter of triangle.


a=int(input(' entyer first side:'))

b=int(input(' entyer second side:'))


c=int(input(' entyer third side:'))
S=(a+b+c)/2
A=(S*(S-a)*(S-b)*(S-c))**0.5
p=(a+b+c)
print(' perimeter of triangle:', p)

print('area of triangle is:',A)

Program 3 : Write a Program to append name in the given below list.


A = ['Joseph','Peter','Cook','Tim']
B=['John','Bob']
print(A+B)
Program 4 : Write a Program to Sort the given dictionary
name={'1:Alice','2:John','4:Peter','3:Andrew','6:Ruffalo','5:Raj'}
tuple(sorted(list(name)))

You might also like