You are on page 1of 29

01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

Python 3.4 Microsoft Visual Studio

1. Microsoft Visual Studio 2015 Python Version 3.4


2. Python 01076249 Data Structures & Algorithms
programming C Java step :
1 :
1.1. MS Visual Studio interactive window 2
1.2. type() 4
1.3. identifier (Name) 4
1.4. Numbers Types & Operations 5
1.5. Assignment Statement 5
1.6. Import Statement 6
1.7. Multiple Assignments 7
1.8. String, Repetition, Concatenation, Indexing, len(), slicing 7
1.9. List, Indexing, slicing, Repetition, Concatenation, len(), append(), Nested List, List Operations 10
2 : Open Python Project MS Visual Studio <F5> <Ctrl+F5>
<F8> (<F11>) <Ctrl+F8> (<F10>) Auto Local Watch
3 : Control Flow
3.1. if 22
3.2. while 24
3.3. range() 24
3.4. for 25
3.5. break, continue, else clauses on loops 25
3.6. pass 26
3.7. Function Definition 26
3.8. Return Value(s) Function 26
4 :
4.1. def factorial(n): 27
4.2. def multiples_of_3_and_5(n): 27
4.3. def integer_right_triangles(p): 27
4.4. def gen_pattern(chars): 27

KMITL . / . / . 1/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1. 1 :
1.1. MS Visual Studio interactive window

icon All Programs


Visual Studio 2015
Start Page

Quick Launch :
interactive window

Python 3.4 Interactive

Interactive
Window
&
prompt
>>>

KMITL . / . / . 2/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

interactive version Python prompt >>> Python


statement
$help <Enter>

KMITL . / . / . 3/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.2. type() : Python OOP type object lecture output


>>> s = 'Hi' # string in single or double quotes
>>> i = 5 # float
>>> f = 3.2 # int
>>> print(s, i, f,'try')

>>> type(s)

>>> type('Hi')

>>> type(i)

>>> type(f)

>>> i #interactive mode print(i)

1.3. identifier (Name) a to z, A to Z, 0to 9, _


keywords

Python Keywords

False and break def else for if is not raise while

None as class del except from import lamda or return with

True assert continue elif finally global in nonlocal pass try yield

KMITL . / . / . 4/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.4. Numbers Types & Operations

operators expression

>>> 5/2
2.5
>>> 5//2
2
>>> 5%2
1
>>> 2**3
8
>>> 3.5 - 2
1.5

1.5. Assignment Statement

C: int a = 5.2; // a data 5.2


// define type a memory type
Python: a = 5.2 # a address object 5.2
# a reference 5.2
assignment statement name reference object id()
operators: is is not

KMITL . / . / . 5/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

>>> a = 5.2
>>> b = int(2)
>>> a is b

>>> print(a, b, id(a), id(b))

>>> a = b
>>> print(a, b, id(a), id(b))

>>> a is b

1.6. Import Statement

math module : file math functions import

import

>>> import math >>> from math import pi


>>> print(math)
>>> print(pi)
<module 'math' (built-in)> name
3.141592653589793
>>> print(math.pi)

3.141592653589793 >>> from math import *
>>> math.cos(math.pi)
>>> cos(pi)
-1.0
-1.0
>>> math.log2(8)
3.0 >>> pi = 5
>>> math.sqrt(16)
>>> cos(pi)
4.0
0.28366218546322625
>>> math.trunc(3.67)
3

KMITL . / . / . 6/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.7. Multiple Assignments

>>> a, b, c = 1, 3.5, 'Hello' >>> i = j = k = 'same'


>>> print(a, b, c) >>> id(i)
1 3.5 Hello 37565440
>>> id(j)
37565440
>>> id(k)
37565440

evaluate expresstion

expr3, expr4 = expr1, expr2


code a b

>>> a = 5
>>> b = 10
>>> a, b = b, a
>>> print(a,b)
10 5

code x[1] 2 i= 1 evaluate x[i] = 2 x[1] = 2

>>> x = [0, 1]
>>> i = 0
>>> i, x[i] = 1, 2
>>> print(x)
[0, 2]

1.8. String

String type character Python string

1.8.1. single quotes double quotes string


>>> 'string'
'string'
>>> "This is also string"
'This is also string'

KMITL . / . / . 7/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.8.2. Repetition ( * ) Concatenation ( + ) Operators

>>> 3*'aa' + 'bcd'


'aaaaaabcd'
>>> str = 'x'
>>> 6*str
'xxxxxx'

1.8.3.String Literals concatenation variable +


>>> '1234''5678'
'12345678'
>>> s2 = '1234'
>>> s2 + '5678'
'12345678'

string

>>> longStr = 'kasdfjkas'


>>> longStr = ('long .........................'
... 'still ........................'
... 'finally.......................')
>>> longStr
'long .........................still ........................finally.......................'

1.8.4.String Indexing (subscript)


character string index 0 1 ...
index - index -1 (-0 0) -2
index (range) error
>>> s = '0123456789'
>>> s[1]
'1'
>>> s[-1]
'9'
>>> s[-2]
'8'
>>> s[20]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range

KMITL . / . / . 8/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.8.5. len() return String Length

>>> s = '0123456789'
>>> len(s)
10

1.8.6.String Slicing
>>> s = '0123456789'
>>> s[1:3]
substring string a:b:c '12'
>>> s[2:9:2]
a index ( ) '2468'
>>> s[:3]
b " ( string) '012'
c" ( 1) >>> s[2:]
'23456789'
>>> s[-7:8]
'34567'
>>> 'Hello' + s[-7:8]
'Hello34567'

1.8.7.String immutable type

type int float (object) string immutable


>>> s = '0123456789'
>>> s[0] = 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

1.8.8. String Operations

KMITL . / . / . 9/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.9. List

List type Python >>> [1,2,3,4]


[1, 2, 3, 4]
[ ] element list , >>> list = ['Hello', 3, 3.5]
>>> print(list)
element type ( type ) ['Hello', 3, 3.5]

1.9.1. Indexing (subscript) Slicing >>> list = ['Hello', 3, 3.5]


>>> list[0]
string type built-in sequence type 'Hello'
>>> list[-1]
List index slicing 3.5

string int float, list mutable type


>>> l = [0,1,2,3,4,5,6,7,8,9]
>>> list = ['Hello', 3, 3.5] >>> l[1:9:2]
>>> list[0] = 1 [1, 3, 5, 7]
>>> list >>> l[-7:len(l):2]
[1, 3, 3.5] [3, 5, 7, 9]
>>> l[:] [0,1,2,3,4,5,6,7,8,9]
>>> l[1:9:2] = ['a','b','c','d']
>>> l
[0, 'a', 2, 'b', 4, 'c', 6, 'd', 8, 9]
>>> l[1:5] = [] # empty list
>>> l
[0, 'c', 6, 'd', 8, 9]

1.9.2. Repeatition ( * ) Concatenation ( + ) Operators len()


>>> li = [1,2]
>>> lis = [3,4,5]
>>> 2*li + lis
[1, 2, 1, 2, 3, 4, 5]
>>> len(li)
2

1.9.3. append() list method >>>


[1,
li
2]
>>> li.append(3)
listVariable.append(i) element i list >>>
[1,
li
2, 3]

>>> li
[1, 2, 3]
1.9.4. Nested List >>> li.append([3,4])
>>> li
[1, 2, 3, [3, 4]]
list >>> li[3]
[3, 4]
>>> li[3][1]
4

KMITL . / . / . 10/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

1.9.5.List Operations

KMITL . / . / . 11/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

2. 2 : Open Python Project MS Visual Studio

2.1. Start Page <New Project>

2.

1.

4.
3.
(QuickPython)

New Project Window : application ( QuickPython) ok

Solution Explorer

Project

Code

(Interactive Window )

KMITL . / . / . 12/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

: / ()

View Menu
( )

icon

compile time error


mouse

KMITL . / . / . 13/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

reset window layout : Menu Window


Solution Explorer

/ Menu Bar


mouse

KMITL . / . / . 14/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

2.2. Line Numbers


Line Numbers

KMITL . / . / . 15/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C


2.3. run Debugger keyboard mouse

shortcut

MS Visual Studio
debugger
debug run

short cut

shortcut

Debug Menu

2.3.1. <Ctrl+F5> debugger run output / error

show Debugger
OUTPUT

<Ctrl+F5> run

show
ERROR

2.3.2. <F5> debugger output / error

<F5> error

error

KMITL . / . / . 16/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

2.3.3. <Shift+F8> <F10> Step Over debugger

2.3.4. <F8> <F11> Step Into debugger run

2.3.5. <Ctrl+Shift+F8> Step Out debugger

<F8>
<F11>



()

<F8>
<F11>

KMITL . / . / . 17/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

<F8> fn definition ( call)


<F11>

<F8>
step into fn
<F11>
fancyPrint()

stack
1. line 7 QuickPython
2. jump fancyPrint()

KMITL . / . / . 18/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

<F8> i=0<3 for


<F11>

n, i

<F8> print output i


<F11>

output i

KMITL . / . / . 19/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

<F8> i = 1 < 3 ( n ) for


<F11>

n, i

<F8> print output i


<F11>

output i

KMITL . / . / . 20/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

step out :
<Ctrl+Shift+
F8>
control ()
output

output

local variables : n, i
fancyPrint()
scope

x local
QuickPython

step over :
<Ctrl+F8>
run fancyPrint(x+5)
<F10>
output

output
call fancyPrint()
2

KMITL . / . / . 21/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

2.4. Stop Debugging debug

KMITL . / . / . 22/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

3. 3 : Control Flow
:
body block
3.1. If 3

1. if

if test expression:
statement(s)

2. if ... else

if test expression:
Body of if
else:
Body of else

3. if ... elif ...else # if ... else if ... else

if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

level nesting indentation


C condition ( )
: block statements

KMITL . / . / . 23/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

input() :
prompt
float() print() :
str() input string end
int()
cast string ( newline)
int ( null character)

x = int(input("Please enter an integer: "))

print(x, end ='')

if x >= 0:

if x == 0:

print('=0')

else:

print('>0')

else:

print('<0')

score = int(input('input your score : '))


if score < 50:
print('fail')
elif score == 50:
print('Ohh!! almost fail')
else: print('pass')

: Python Code 3 test case

:
Multiple Assignment !

x, y, z = 5,7,3 x, y, z = 15,7,20 !
if x > y and x > z : if x > y > z :
print('max = ',x) print('max = ',x)
elif y > x and y > z : elif y > x > z :
print('max = ',y) print('max = ',y)
else: print('max = ',z) else: print('max = ',z)

KMITL . / . / . 24/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

3.2. while

while test_expression:
Body of while

# initialize sum and counter


sum = 0
i = 1

while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)

3.3. range()

a = start
b = end
s = step size ( 1)

range (a, b, s) return sequence a, a + 1s, a + 2s, a + 3s, . . ., before b

argument n

a = 0, b = n, s = 1

print(list(range(5))) [0, 1, 2, 3, 4]

print(list(range(0,5))) [0, 1, 2, 3, 4]

print(list(range(-10,-50,-20))) [-10, -30]

KMITL . / . / . 25/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

3.4. for variable


sequence
iteration

for val in sequence:


Body of for

# Find sum of elements in list


list = [2, 1, 3, 4]
sum = 0

for ele in list: # iterate over the list


sum = sum + ele

print("Sum =", sum) Sum = 10

for i in range(5): 0 1 2 3 4
print(i, end = ' ')
print()

list[0] = 2
list[1] = 1
list[2] = 3
? list[3] = 4

3.5. break , continue, else clauses on loops


for n in range(2, 10):
for x in range(2, n):
break if n % x == 0:
loop print(n, '=', x, '*', n//x)
else loop break
else:
# loop fell through without finding a factor
print(n, 'is prime')

else continue
for iteration
2 is prime iteration
while condition false 3 is prime
4 = 2 * 2
5 is prime
6 = 2 * 3
7 is prime
8 = 2 * 4
9 = 3 * 3

KMITL . / . / . 26/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

3.6. pass
pass
class myClass:
pass

def myFun(n):
pass

3.7. Function Definition

3.8. Return Value(s) Function

KMITL . / . / . 27/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

4. 4 :

comment raise statement

4.1. factorial

4.2. n mutiples 3 5 multiples 3 5 10


3 5 6 9 23

4.3. 60 (right triangle) 2 [(10, 24, 26),


(15, 20, 25)] code integer p returns list tuples (a, b, c)
tuple ab<c list solution
a

4.4. gen_pattern() pattern string 1


ASCII art pattern character return

KMITL . / . / . 28/23
01076250 Data Structures & Algorithms Laboratory Lab 1 : A Brief C

return pattern string ( print ) newline


Python '\n' pattern return str '..Y..\nY.X.Y\n..Y..'

2 type str join center :

KMITL . / . / . 29/23

You might also like