Unit 5-Python Packages 240127 185930
Unit 5-Python Packages 240127 185930
• GUI Programming
– Tkinter Introduction,
– Tkinter and Python Programming
– Tk widgets
– Tkinter Example
import numpy as np
arr=np.array([1,2,3,4])
arr[1]=5
print(arr) # print [1 5 3 4]
print(type(arr)) # print <class 'numpy.ndarray'>
# slicing
print(arr[0:3]) # print [1 5 3]
arange() function
Converts number in given range to array
import numpy as np
arr=np.arange(0,5)
print(arr) print [0 1 2 3 4]
print(arr.shape) print (4,)
arr=np.array([[1,2],[2,2]])
print(arr.shape) print (2,2)
Installation of Pandas
• If you have Python and PIP already installed on a system, then installation
of Pandas is very easy.
• Install it using this command:
C:\Users\Your Name>pip install pandas
• If this command fails, then use a python distribution that already has
Pandas installed like, Anaconda, Spyder etc.
Import Pandas
• Once Pandas is installed, import it in your applications by
adding the import keyword:
Import pandas as pd # pd is alias for pandas
dataset = { 'cars': ["BMW", "Volvo", "Ford"], 'passings': [3, 7, 2]}
var1 = pd.DataFrame(ataset, index=['Row 1','Row 2','Row 3'])
print(var1)
# Output
cars passings
Row 1 BMW 3
Row 2 Volvo 7
Row 3 Ford 2
DataFrame-Explanation
A Data frame is a two-dimensional data structure, i.e., data is
aligned in a tabular fashion in rows and columns.
Features of DataFrame
• Potentially columns are of different types
• Size – Mutable
• Labeled axes (rows and columns)
• Can Perform Arithmetic operations on rows and
columns
The syntax of creating a DataFrame object is:
pandas.DataFrame(Data, index, columns, dtype, copy)
Data: It can be represented as series, list, dictionary, constant
or other DataFrames.
index: For the row labels, the index to be used for the
resulting frame is optional. The default value is displayed from
0 to n-1.
columns: For column labels, The optional default syntax is :
np.arange(n).
dtype: It is the data type of each column. If no data type is
defined. None is applied.
copy: This command is used for copying data if the default is
false.
Series() method
• Pandas Series is a one-dimensional labeled array capable of holding
data of any type (integer, string, float, python objects, etc.).
import pandas as pd
# simple array
data = [1, 2, 3, 4]
ser = pd.Series(data)
print(ser)
# Output
01
12
23
34
dtype: int64
GUI Programming
Tkinter Introduction :
Tkinter is an open source, portable graphical user interface (GUI)
toolkit designed for use in Python scripts.
„Tkinter is a python interface for the Tk GUI toolkit (originally
developed for the Tcl language)
Advantages offered by Tkinter
„Layered implementation
„Accessibility
„Portability
„Availability
Drawback of Tkinter
„ Due to the layered approach used in its implementation,
execution speed becomes a concern.
Tkinter and Python Programming
# install tkinter before use program executed on pycharm IDE
# Basic Program
from tkinter import *
root = Tk( )
root.title("A simple application") # Displays title on the title bar of window
root.mainloop( )
# Output
Tkinter and Python Programming
from tkinter import * : Imports everything from tkinter library
Tk( ):
In order to create a tkinter application, we generally create an
instance of tkinter frame, i.e., Tk().
It helps to display the root window and manages all the other
components of the tkinter application.
We can initialize the tkinter instance by assigning the variable
to it
mainloop():
mainloop() is used when your application is ready to run.
mainloop() is an infinite loop used to run the application, wait
for an event to occur and process the event as long as the
window is not closed.
Tkinter and Python Programming
# We can place graphical components on the window like label,
button etc
from tkinter import *
root = Tk( )
root.title("A simple application")
# Creating Label
label=Label(root, text="Python Label", font=('Courier 20
underline'))
label.pack() # Adding Label to root window
# Creating button
button = Button(root, text="Press")
# Adding button
button.pack()
root.mainloop( )
Output
Tk widgets