1 of 29

BasicsPython Programming

Satishkumar L. Varma

Professor, Department of Computer Engineering

PCE, New Panvel

www.sites.google.com/site/vsat2k

2 of 29

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • What is Python
  • IDE
  • Basics
  • GUI Library
  • Tkinter Programming
  • Tkinter Widgets
  • Add Widget GUI

2

3 of 29

What is Python

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

3

  • Python created by Guido van Rossum and first released in 1991
  • Employed by Google from 2005 until Dec 2012
  • He started working on Dropbox in 2013
  • Use English keywords rather than punctuation
  • Origin of name comes from Monty Python
  • Platform independent
  • Interpreted language
  • https://www.python.org/downloads/

4 of 29

IDE

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • IDLE
  • PythonWin
  • PyCharm
  • Wing
  • Spider

4

5 of 29

Python Programs: Basics

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Hello Python
  • Input and Print
  • Operands
  • Control Statements
  • Functions
  • List and operations
  • String and Operations
  • File IO

5

6 of 29

Python Programs: Hello Python

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# This line is commented

"This is my first program"

'''This is my multiline comment'''

'''

Developer:

Date:

Contact:

'''

print ("Hello Python!")

6

7 of 29

Python Programs: Input and Print

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

#input and print

thetext = input("Enter some text: ")

print ("This is what you entered: ")

print (thetext)

# \n within quote is for a new line

thetext = input("Enter some text\n")

print ("This is what you entered:")

print (thetext)

# another method

prompt = "Enter some text "

thetext = input(prompt)

print ("This is what you entered:")

print (thetext)

7

8 of 29

Python Programs: Operands

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# integer, floating point number and operands

total = 0.0

number1=float(input("Enter the first number: "))

total = total + number1

number2=float(input("Enter the second number: "))

total = total + number2

average = total / 2

print ("The average is " + str(average))

print (average)

8

9 of 29

Python Programs: Operands

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# type of data

total = 10

print (total)

print (type (total))

# operands on integer

print (2 + 4)

print (6 - 4)

print (6 * 3)

print (6 / 3)

print (6 % 3)

print (6 // 3) # floor division: always truncates fractional remainders

print (-5)

print (3**2) # three to the power of 2

9

10 of 29

Python Programs: Operands

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# operands on integer

print (2.0 + 4.0)

print (6.0 - 4.0)

print (6.0 * 3)

print (6.0 / 3.0)

print (6.0 % 3.0)

print (6.0 // 3.0) # floor division: always truncates fractional remainders

print (-5.0)

print (3.0**2.0) # three to the power of 2

10

11 of 29

Python Programs: Operands

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# Boolean expressions result in a value of true/false

# Python stores true as int 1, and false as int 0

# It outputs 'true' or 'false' from print statements

print (7 > 9)

print (10 < 16)

print (5 == 5)

print (6 <= 6)

print (6 >= 6)

print (10 != 10)

11

12 of 29

Python Programs: Operands

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

a = 'Hello Python. '

b = "Where are you?"

c = a + b

print (c)

# d = c + 10

# Cannot concatenate a string and an integer

# Convert the integer to a string first:

d = c + str(100)

print (d)

12

13 of 29

Python Programs: Control Statements

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# if else statement

myNum = int(input("Enter the first number: "))

if myNum < 10:

print('entered number is less than 10.')

elif myNum > 10 and myNum < 50:

print('entered number is between 10 and 50.')

else:

print('entered number is more than 50.')

print('square of entered number is = ', str(myNum*myNum))

13

14 of 29

Python Programs: Control Statements

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# Control statement

total = 0.0

count = 0

while count < 2:

number=float(input("Enter a number: "))

count = count + 1

total = total + number

average = total / count

print ("The average is " + str(average))

14

15 of 29

Python Programs: Function without arguments

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

Function without arguments

# Using function without arguments

def mysum():

a = input("Enter value of A = ")

b = input("Enter value of B = ")

s = int(a) + int(b)

return s

s = mysum()

print("The sume is = ", s)

15

16 of 29

Python Programs: Function with arguments

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# Using function with arguments

def calc(a, b):

s = int(a) + int(b)

m = int(a) * int(b)

return s,m

a = input("Enter value of A = ")

b = input("Enter value of B = ")

s = int(a) + int(b)

s,m = calc(a, b)

print("The multiplication is = ", m)

16

17 of 29

Python Programs: List Creation

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# creating and using a Python list

result = [0,0,0,0,0,0,0,0]

print (result)

result[0] = 17

result[1] = 20

result[5] = 50

print (result)

print (result[0])

print (result[1])

print (result[2])

print (result[3])

print (result[4])

print (result[5])

print (result[6])

print (result[7])

17

18 of 29

Python Programs: List operations

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# List operations

myList = []

print (myList)

myList.append(100)

print (myList[0])

myList.append("vsat2k")

print (myList)

print (myList[0])

print (myList[1])

# the following statement would generate an out-of-range error

#print (myList[2])

18

19 of 29

Python Programs: Access and delete item

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# accessing the last item in a list

list1 = [1,2,3,6,7,8,9,10]

print (list1)

print (list1[0])

print (list1[1])

print (list1[-1])

print (list1[-2])

# deleting items from a list

list1 = [1,2,3,4,5,6,7,8,9,10]

print (list1)

del list1[0]

del list1[-1]

print (list1)

19

20 of 29

Python Programs: Repeat and Concatenate

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# repeating lists

list1 = [1,2,3]

print (list1)

print (list1 * 3)

print (list1)

list1 = list1 * 2

print (list1)

# concatenating lists

list1 = [1,2,3]

print (list1)

list2 = [4,5,6]

print (list2)

list1 = list1 + list2

print (list1)

list1 = list1 + list1

print (list1)

20

21 of 29

Python Programs: String and Operations

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# String operations

print ("This is A")

print ("This is B's A")

# You can also print a " within a string enclosed in single quotes:

print ('One double quote ", and "within double quote" ')

# multiplying numbers and strings

print (3 * 4)

print (30 * 4)

print ("3" * 4)

print ("30" * 4)

# string concatenation

print ("vsat2k " + "is " + ("email id " * 3))

# string indexing

s1 = "Satishkumar Varma"

print (s1[0],s1[5])

21

22 of 29

Python Programs: File IO

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

#Directory Command

import os

# Check current working directory.

cwd = os.getcwd()

print("Current working directory %s" % cwd)

# Now change the directory

path = cwd + "\satish"

os.chdir(path)

# Check current working directory.

newPath = os.getcwd()

print("Directory changed successfully %s" % newPath)

22

23 of 29

Python Programs: Open and read file

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# Open and read file

fp = open('myFile.txt','r')

# fp = open('D:\\vsat2k\\myFile.txt','r')

string = fp.readline()

string = fp.readline()

print (string)

23

24 of 29

Python Programs: Open and read file

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# Open and read file

fp = open('myFile.txt','r')

# fp = open('D:\\vsat2k\\myFile.txt','r')

string = fp.readline()

string = fp.readline()

string = fp.read(10)

string = fp.read(12)

print (string)

24

25 of 29

Python Programs: Open, write and close file

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

# Open, write and close file

fp = open('myFile.txt','w')

# fp = open('D:\\vsat2k\\myFile.txt','r')

print (fp) # prints out details about the file

fp.write("Today is Thu\n")

fp.write("I am learning Python.\n")

fp.close()

25

26 of 29

Python Programs: Open, read and write

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

def makeCopy(oldFile, newFile):

fp1 = open(oldFile, "r")

fp2 = open(newFile, "w")

while 1:

content = fp1.read(50)

if content == "":

break

fp2.write(content)

fp1.close()

fp2.close()

return

file1 = "myFile.txt" # existing file

file2 = "copy_myFile.txt" # copy of existing file

makeCopy(file1, file2)

26

27 of 29

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

27

28 of 29

References

  • Zed A. Shaw, “Learn Python the Hard Way”, Third Edition, Addison-Wesley, 2014.
  • Bruno Dufour, “A Comprehensive Introduction to Python Programming and GUI Design Using Tkinter”, McGill Univeristy SOCS.
  • JOHN E. GRAYSON, “Python and Tkinter Programming”, Manning Publications, 2000.
  • Burkhard A. Meier, “Python GUI Programming Cookbook”, Packt Publishing, 2015.

28

Satishkumar Varma, PCE www.sites.google.com/site/vsat2k

29 of 29

Thank You.

Satishkumar Varma, PCE www.sites.google.com/site/vsat2k

29