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

Python Atb

The document summarizes key features of the Python programming language. It discusses that Python is a simple, free and open source language that is portable, extensible, high-level and object-oriented. It can be used for web applications, connecting to databases, handling big data, data mining, and more. The document also covers Python data types, functions, operators, strings, lists, tuples, and conversions between data structures.

Uploaded by

Kunwar Aditya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Python Atb

The document summarizes key features of the Python programming language. It discusses that Python is a simple, free and open source language that is portable, extensible, high-level and object-oriented. It can be used for web applications, connecting to databases, handling big data, data mining, and more. The document also covers Python data types, functions, operators, strings, lists, tuples, and conversions between data structures.

Uploaded by

Kunwar Aditya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Features of Python

1. Simple Language:

Coding in Python is very simple. A user with the non-coding background can easily learn this language if
they have the good logical skill as python use basic English language for coding.

2. Free and Open Source:

Python is a freeware and open source software so it can be easily downloaded and used. It doesn’t require
any activation key or subscription to work on it.

3. Portability :

Python can run any operating system. Also, we use the python code written on one system onto another
system without making any changes to the code.

4. Extensible and Embeddable:

This feature is provided to the user as to extend python from its initial state.

An extensible software program, for example, might support add-ons or plug-ins that add extra functionality
to the program.

5. High-level Interpreted Language:

In this feature the code which execute instruction directly and freely, without previously compiling a
program into machine language instructions.

6. Object Oriented :

Python is an object oriented programming language. Unlike procedure oriented programming,


where the main emphasis is on functions, object oriented programming stress on objects. Object is
simply a collection of data (variables) and methods (functions) that act on those data.

Python Functionality

1. It can be used on a server to create web applications.

2. It can be used to connect to the database system and also perform a various task like read and modify.

3. It can be used to handle big data and perform complex mathematics.

4. It can also be used for data mining process.

5. It can be used for rapid prototyping, or for production-ready software development.


Python has five standard data types that are used to define the operations possible on them and the storage
method for each of them.

• Number

• String

• List(A list can store a sequence of objects in a certain order such that you can index into the list, or
iterate over the list. List is a mutable type meaning that lists can be modified after they have been
created.)

• Tuple(A tuple is similar to a list except it is immutable. There is also a semantic difference between
a list and a tuple)

• Dictionary(A dictionary is a key-value store. It is not ordered and it requires that the keys are
hashable. It is fast for lookups by key)

LIST TUPLES
Lists are mutable i.e they can be edited. Tuples are immutable (tuples are lists which can’t be edited).
Lists are slower than tuples. Tuples are faster than list.
Syntax: list_1 = [10, ‘Chelsea’, 20] Syntax: tup_1 = (10, ‘Chelsea’ , 20)

What is Pass statement in Python?

It is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes.

What is the significance of Indentation in Python?

Indentation is required for Python. It specifies a block of code. All code within loops, classes, functions, etc
is specified within an indented block. If python code is not indented necessarily, it will not execute
accurately and will throw errors as well.

What is the difference between Arrays and lists in Python ?

Arrays and lists store data in same way . But, arrays can hold only a single data type elements whereas lists
can hold any data type elements.

Explain pickling and unpickling?

Pickle module accepts any Python object and converts it into a string representation and dumps it into a file
by using dump function, this process is called pickling. While the process of retrieving original Python
objects from the stored string representation is called unpickling.
Number data types store numeric values. Number objects are created when you assign a value to them. For
example

var1= 1
var2 = 10

You can also delete the reference to a number object by using the del statement. The

For example

del var1
del var1, var2

Expression and Statement Forms

x=0.6

x=3.9 * x * (1-x)

print (round(x, 2))

Basic Operators in Python

Operators are the constructs that can manipulate the value of operands. Like different programming
languages, Python supports the following operators:

• Arithmetic operators

• Relational operators

• Assign operators

• Logical operators

Arithmetic Operators

Performs floor division (gives the integer value after division)

print (13//5)

Perform calculation

print (13+5) ;

print (13-5) ;

print (2*5) ;
print (13/5);

print (13%5)

print (2**3)

Relational Operators

examples of relational operators in Python.

print(13<5) ; print(13>5); print(13<=5); print(2>=5); print(13==5); print(13!=5)

Assign Operator

X =10 ; print(X)

x=10; x/=2; print(x)

x=10; x+=7; print(x)

x=10; x-=6; print(x)

Logical Operators

x= 10>5 and 4>20 ; print (x)

x= 10>5 or 4>20 ; print (x)

x= not (10<4) ; print (x)

import calendar

calendar.prcal(2019)

Python Strings

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

A string is a sequence of characters that can be accessed by an expression in brackets called an


index. For instance, if you have a string variable named var1, which maintains the word PYTHON,
then var1[1] will return the character Y, while var1[-2] will return the character O. Python considers
strings by enclosing text in single as well as double quotes.Strings are stored in a contiguous
memory location that can be accessed from both directions (forward and backward), as shown in the
following example, where
• Forward indexing starts with 0, 1, 2, 3, and so on.
• Backward indexing starts with -1, -2, -3, -4, and so on.

s="PYTHON"
print (s[0:4])
print (s[1:3])
print (s[:2])
print (s[2:])
print (s [:])
print (s)
print(s[-4:-1])

Update String

You can "update" an existing string by (re)assigning a variable to another string. The new value can be
related to its previous value or to a completely different string altogether.
For example-

var1 = 'Hello World!'

print ("Updated String :- ", var1[:6] + 'Python')

Python Lists

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and
enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One of the differences
between them is that all the items belonging to a list can be of different data type. The values stored in a 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-
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

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

Next Example

list1 = ['Egypt', 'chemistry', 2017, 2018]

list2 = [1, 2, 3, [4, 5]]


list3 = ["a", 3.7, '330', "Omar"]
print (list1[2])

print (list2 [3:])


print (list3 [-3:-1])
print (list3[-3])

UPDATE

list = ['physics', 'chemistry', 1997, 2000]


print ("Value available at index 2 : ", list[2])
list[2] = 2001
print ("New value available at index 2 : ", list[2])

DELETE

list = ['physics', 'chemistry', 1997, 2000]


print (list)
del list[2]
print ("After deleting value at index 2 : ", list)

LIST Indexing

L=['C++'', 'Java', 'Python']


Examples of nested List

list1 = [ ‘Asis’,’Rani’, ‘Attman’, [‘Ramanjeet’, ‘Paras’, ‘Abhijit’], ‘Aditya’]

print(list1[3])

print(list1[3][1])

Arrays and List

import array as JRH


MJ_Array=JRH.array('i',[10,11,12,13])
MJ_list=[1,'pqr',1.25]
print(MJ_Array)
print(MJ_list)

Conversion

aTuple = (123, 'C++', 'Java', 'Python')

list1 = list(aTuple)
print ("List elements : ", list1) str="Hello World"
list2=list(str)
print ("List elements : ", list2)

Python Tuples

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values
separated by commas. Unlike lists, however, tuples are enclosed within parenthesis. The main difference
between lists and tuples is- 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-

tup1 = ('physics', 'chemistry', 1997, 2000)


tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
example 2

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


tinytuple = (123, 'john')
print (tuple) # Prints complete tuple
print (tuple[0]) # Prints first element of the tuple
print (tuple[1:3]) # Prints elements starting from 2nd till 3rd
print (tuple[2:]) # Prints elements starting from 3rd element
print (tinytuple * 2) # Prints tuple two times
print (tuple + tinytuple) # Prints concatenated tuple

The following code is invalid with tuple, because we attempted to update a tuple, which is not allowed.
Similar case is possible with lists –

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


list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list

Python Dictionary

Python's 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.
Prices = {"Honda":40000, "Suzuki":50000, "Mercedes":85000, "Nissan":35000,
"Mitsubishi": 43000}
print (Prices)

Staff_Salary = { 'Omar Ahmed' : 30000 , 'Ali Ziad' :24000,'Ossama Hashim': 25000, 'Majid
Hatem':10000}
print(Staff_Salary)
STDMarks={"Salwa Ahmed":50, "Abdullah Mohamed":80, "Sultan Ghanim":90}
print(STDMarks)

next examples

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}


print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])

example 2

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

Dictionaries have no concept of order among the elements. It is incorrect to say that the elements are
"out of order"; they are simply unordered.

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8; #


update existing entry dict['School'] = "DPS School" # Add new
entry
print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])

next examples
Updating and Adding a New Item to a Dictionary

STDMarks={"Salwa Ahmed":50, "Abdullah Mohamed":80, "Sultan Ghanim":90}


STDMarks['Salwa Ahmed'] = 85 # update current value of the key 'Salwa Ahmed'
STDMarks['Omar Majid'] = 74 # Add a new item to the dictionary
print (STDMarks)

Tabular Data and Data Formats

Data is available in different forms. It can be unstructured data, semistructured data, or


structured data. Python provides different structures to maintain data and to manipulate it such as
variables, lists, dictionaries, tuples, series, panels, and data frames. Tabular data can be easily
represented in Python using lists of tuples representing the records of the data set in a data frame
structure. Though easy to create, these kinds of representations typically do not enable important
tabular data manipulations, such as efficient column selection, matrix mathematics, or spreadsheet-
style operations.
Tabular is a package of Python modules for working with tabular data. Its main object is
the tabarray class, which is a data structure for holding and manipulating tabular data. You can put
data into a tabarrayobject for more flexible and powerful data processing.
The Pandas library also provides rich data structures and functions designed to make working with
structured data fast, easy, and expressive. In addition, it provides a powerful and productive data
analysis environment.
A Pandas data frame can be created using the following constructor:
pandas.DataFrame( data, index, columns, dtype, copy)

A Pandas data frame can be created using various input forms such as the following:
• List
• Dictionary
• Series
• Numpy ndarrays
• Another data frame

Python Pandas Data Science Library


Pandas is an open source Python library providing high-performance data manipulation and analysis
tools via its powerful data structures. The name Pandas is derived from “panel data,” an econometrics
term from multidimensional data. The following are the key features of the Pandas library:

 Provides a mechanism to load data objects from different formats


 Creates efficient data frame objects with default and customized
indexing
 Reshapes and pivots date sets
 Provides efficient mechanisms to handle missing data
 Merges, groups by, aggregates, and transforms data
 Manipulates large data sets by implementing various
functionalities such as slicing, indexing, sub setting , deletion, and
insertion
 Provides efficient time series functionality

Sometimes you have to import the Pandas package since the standard Python distribution doesn’t
come bundled with the Pandas module. A lightweight alternative is to install Numpy using popular
the Python package installer pip. The Pandas library is used to create and process series, data frames,
and panels.

A Pandas Series

A series is a one-dimensional labeled array capable of holding data of any type (integer, string,
float, Python objects, etc.). Listings hows how to create a series using the Pandas library.
Creating a Series Using the Pandas Library

#Create series from array using pandas and numpy

import pandas as pd
import numpy as np
data = np.array([90,75,50,66])
s = pd.Series(data,index=['A','B','C','D'])
print (s)
A 90
B 75
C 50
D 66
print (s[1])
75

#Create series from dictionary using pandas


import pandas as pd
import numpy as np
data = {'Ahmed' : 92, 'Ali' : 55, 'Omar' : 83}
s = pd.Series(data,index=['Ali','Ahmed','Omar'])
print (s)

Ali 55
Ahmed 92
Omar 83

print (s[1:])

Ahmed 92
Omar 83

A Pandas Data Frame

A data frame is a two-dimensional data structure. In other words, data is aligned in a tabular
fashion in rows and columns. In the following table, you have two columns and three rows of data.
Listing shows how to create a data frame using the Pandas library.

Name Age

ahmed 35
ali 17
omar 25

Listing . Creating a Data Frame Using the Pandas Library


import pandas as pd
data = [['Ahmed',35],['Ali',17],['Omar',25]]
DataFrame1 =pd.DataFrame(data,columns=['Name','Age'])
print (DataFrame1)

You can retrieve data from a data frame starting from index 1 up to the end of rows.

DataFrame1[1:]

Name Age
1 Ali 17
2 Omar 25

You can create a data frame using a dictionary.

import pandas as pd
data = {'Name':['Ahmed', 'Ali', 'Omar','Salwa'],'Age':[35,17,25,30]}
dataframe2 = pd.DataFrame(data, index=[100, 101, 102, 103])
print (dataframe2)

Age Name
100 35 Ahmed
101 17 Ali
102 25 Omar
103 30 Salwa

You can select only the first two rows in a data frame.
dataframe2[:2]

Age Name
100 35 Ahmed
101 17 Ali

You can select only the namecolumn in a data frame.

dataframe2['Name']

100 Ahmed
101 Ali
102 Omar
103 Salwa
Name: Name, dtype: object

Key Differences:

 Pandas provides us with some powerful objects like DataFrames and Series which are very useful
for working with and analyzing data whereas numpy library which provides objects for multi-
dimensional arrays, Pandas provides in-memory 2d table object called Dataframe.

 Pandas works when data is in Tabular Format whereas Numpy works really well when data is
Numeric.

 Numpy consumes less memory as compared to Pandas.

 Numpy performs real good when there are 50000 and less rows whereas pandas works really well
when there are around 500000 or more rows.

Decision Making in Python

If else Statement

a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
The elif Statement

amount=int(input("Enter amount: "))


if amount<1000:
discount=amount*0.05
print ("Discount",discount)
elif amount<5000:
discount=amount*0.10
print ("Discount",discount)
else:
discount=amount*0.15
print ("Discount",discount)
print ("Net payable:",amount-discount)

Nested IF Statements

LOOP
While Loop

count = 0

while (count < 9):


print ('The count is:', count)
count = count + 1
print ("Good bye!")

FOR Loop

fruits = ['banana', 'apple', 'mango']


for index in range(len(fruits)):
print ('Current fruit :', fruits[index])
print ("Good bye!")

Use of Break

numbers=[11,33,55,39,55,70,37,21,23,41,13]
for num in numbers:
if num%2==0:
print ('the list contains an even number')
break
else:
print ('the list does not contain even number')

Use of Continue

for letter in 'Python': # First Example


if letter == 'h':
continue
print ('Current Letter :', letter)

var = 10 # Second Example


while var > 0:
var = var -1
if var == 5:
continue
print ('Current variable value :', var)
print ("Good bye!")
for a in range (1,4):
print ( a )
while Iteration Statement

ticket=4
while ticket>0:
print ("Your ticket number is ", ticket) ticket -=1

Python Numpy Package

Numpy is a Python package that stands for “numerical Python.” It is a library consisting of
multidimensional array objects and a collection of routines for processing arrays.
The Numpy library is used to apply the following operations:
• Operations related to linear algebra and random number generation
• Mathematical and logical operations on arrays
• Fourier transforms and routines for shape manipulation
For instance, you can create arrays and perform various operations such as adding or subtracting
arrays, as shown in Listing

Listing . Example of the Numpy Function

a=np.array([[1,2,3],[4,5,6]])
b=np.array([[7,8,9],[10,11,12]])
np.add(a,b)
array([[ 8, 10, 12], [14, 16, 18]])
np.subtract(a,b) #Same as a-b
array([[-6, -6, -6], [-6, -6, -6]])
Data Visualization
A fundamental part of the data scientist’s toolkit is data visualization. Although it is very easy to create
visualizations, it’s much harder to produce good ones. There are two primary uses for data visualization:
To explore data
To communicate data

Matplotlib
This is useful for simple bar charts, line charts, and scatterplots. In particular, we will be using the
matplotlib.pyplot module. In its simplest use, pyplot maintains an internal state in which you build up a
visualization step by step. Once you’re done, you can save it (with savefig()) or display it (with show()).
import matplotlib.pyplot as plt

# values of x and y axes


x = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')

plt.show()

In the first example, the x-axis and y-axis were divided by the value of 10 and 2 respectively. Let’s make it 5
and 1.
import matplotlib.pyplot as plt
import numpy as np
x = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]

plt.plot(x, y, 'b')
plt.xlabel('x')
plt.ylabel('y')
plt.xticks(np.arange(0, 51, 5))
plt.yticks(np.arange(0, 11, 1))
plt.show()

import matplotlib.pyplot as plt


years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
# create a line chart, years on x-axis, gdp on y-axis
plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
# add a title
plt.title("Nominal GDP")
# add a label to the y-axis
plt.ylabel("Billions of $")
plt.show()

Bar Chart

movies = ["Annie Hall", "Ben-Hur", "Casablanca", "Gandhi", "West Side Story"]


num_oscars = [5, 11, 3, 8, 10]
# bars are by default width 0.8, so we'll add 0.1 to the left coordinates
# so that each bar is centered
xs = [i + 0.1 for i, _ in enumerate(movies)]
# plot bars with left x-coordinates [xs], heights [num_oscars]
plt.bar(xs, num_oscars)
plt.ylabel("# of Academy Awards")
plt.title("My Favorite Movies")
# label x-axis with movie names at bar centers
plt.xticks([i + 0.5 for i, _ in enumerate(movies)], movies)
plt.show()

A bar chart can also be a good choice for plotting histograms of bucketed numeric values,in order to visually
explore how the values are distributed

Scatterplots
A scatterplot is the right choice for visualizing the relationship between two paired sets of
data. For example, Figure illustrates the relationship between the number of friends
your users have and the number of minutes they spend on the site every day:
friends = [ 70, 65, 72, 63, 71, 64, 60, 64, 67]
minutes = [175, 170, 205, 120, 220, 130, 105, 145, 190]
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
plt.scatter(friends, minutes)
# label each point
for label, friend_count, minute_count in zip(labels, friends, minutes):
plt.annotate(label,xy=(friend_count, minute_count), # put the label with its point
xytext=(5, -5), # but slightly offset
textcoords='offset points')
plt.title("Daily Minutes vs. Number of Friends")
plt.xlabel("# of friends")
plt.ylabel("daily minutes spent on the site")
plt.show()

You might also like