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

Python Programming: Author: Tigran Khazhakyan

Uploaded by

lusin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Python Programming: Author: Tigran Khazhakyan

Uploaded by

lusin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Python Programming

Author: Tigran Khazhakyan

© 2015 Tigran Khazhakyan All Rights Reserved


Introduction to Python

© 2015 Tigran Khazhakyan All Rights Reserved


Introduction to Python

Python is a widely used general-purpose, high-level


programming language, which appeared in 1991

Guido van Rossum, the Python Software Foundation, the


creator of Python current developer of Python

© 2015 Tigran Khazhakyan All Rights Reserved


Python Usage

© 2015 Tigran Khazhakyan All Rights Reserved


Python Tools

Name Link
Python Interpreter https://www.python.org/downloads/

PyCharm IDE https://www.jetbrains.com/pycharm/

PyDev IDE http://pydev.org/download.html

© 2015 Tigran Khazhakyan All Rights Reserved


Integers

• Integers are immutable, but due to Python’s augmented


assignment this is rarely noticeable.
• Integer size is limited by machine’s memory.

>>> import sys


>>> sys.maxint

Decimal Binary Octal Hexadecimal

>>> 14600926 >>>


0b110111101100101011 >>> 0o67545336 >>> 0xDECADE
14600926 011110 14600926 14600926
14600926

© 2015 Tigran Khazhakyan All Rights Reserved


Creating Integers

X = 17
X = int() # X = 0
X = int(17) # X = 17
X = int(17.6) # X = 17, conversion
X = int(“string”) # ValueError
X = int(“A4”, 16) # X = 164, conversion
X = int(“16”, 8) # X = 14, conversion

© 2015 Tigran Khazhakyan All Rights Reserved


Numeric Operations
Syntax Description
x+y Addition
x–y Subtraction
x*y Multiplication
x/y Division (produces also float)
x // y Division (produces only int)
x%y Produces remainder
x ** y X to power of y
-x Negates x if non-zero
+x Clarifies code. Does nothing
abs(x) Absolute of x
divmod(x, y) Returns quotient and
remainder as a tuple
pow(x, y) The same as **
pow(x, y, z) (X**y)%z
© 2015 Tigran Khazhakyan All Rights Reserved
round(x, n) Rounds to n decimal places
Integer Conversion

bin(10) -> ‘0b1010’ is a string


hex(145) -> ‘0x91’ is a string
int(14.6) -> 14 is an int
int(“16”, 8) -> 14 is an int from octal
oct(230) -> 0346 is an octal of 230

© 2015 Tigran Khazhakyan All Rights Reserved


Booleans

False

bool >>> bool() -> False

>>> bool(True) -> True


True
>>> bool(9) -> True

© 2015 Tigran Khazhakyan All Rights Reserved


Floating-Point Types

float 0.0, 4., 5.7, -2.5, -2e9, 8.9e-4

complex 4 + 5j, -2j

© 2015 Tigran Khazhakyan All Rights Reserved


Strings

‘Hello “Wo”rld!’

“Hello World!”

0 1 2 3 4 var = “Hello World!”


H E L L 0 var[0] -> ‘H’

© 2015 Tigran Khazhakyan All Rights Reserved


Strings’ Special Operators

“Hello” + “World” -> ‘HelloWorld’


“Hello” * 2 -> ‘HelloHello’

“Hello”*1+ -> ‘e’

“Hello”*1:+ -> ‘ello’

“H” in “Hello” -> True

“B” not in “Hello” -> True


© 2015 Tigran Khazhakyan All Rights Reserved
Strings Methods

mystr = “hello world!”

mystr.capitalize() “Hello world!”

mystr.count(‘o’) 2

mystr.isalpha() False

‘-’.join((‘a’, ‘b’, ‘c’)) a-b-c

© 2015 Tigran Khazhakyan All Rights Reserved


Lists
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];


List declaration
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]


List accessing
print "list2[1:5]: ", list2[1:5]

list2[2] = 2001;
print list2; List updating

list1[len(list1):] = 5
Delete list element
del list1[2];
print list1;
© 2015 Tigran Khazhakyan All Rights Reserved
Lists Operations

Python Expression Results Description


len([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]: 123 Iteration
print x,

© 2015 Tigran Khazhakyan All Rights Reserved


Tuples

#!/usr/bin/python

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


tuple2 = (1, 2, 3, 4, 5 )
tuple3 = "a", "b", "c", "d"
tuple1 = ()

print "tuple1[0]: ", tuple1[0]


del tuple1
print tuple1

© 2015 Tigran Khazhakyan All Rights Reserved


Dictionaries
#!/usr/bin/python

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}


dict1 = { 'abc': 456 }
dict2 = { 'abc': 123, 98.6: 37 }

#!/usr/bin/python

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


dict['School'] = "DPS School"
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

del dict['Name']; print dict


dict.clear(); print dict
© 2015 Tigran Khazhakyan All Rights Reserved
Conditional Statements

#!/usr/bin/python #!/usr/bin/python

if boolean statement: if boolean statement1:


#code #code
else: elif boolean statement2:
#code #code
else boolean statement3:
#code

© 2015 Tigran Khazhakyan All Rights Reserved


While Loop

#!/usr/bin/python

while expression:
statement(s)

© 2015 Tigran Khazhakyan All Rights Reserved


For Loop

#!/usr/bin/python #!/usr/bin/python

for num in range(0, 20): for num in range(0, 20):


statement(s) statement(s)
else:
#!/usr/bin/python
statement(s)
somelist = *…+
for item in somelist:
statement(s)
© 2015 Tigran Khazhakyan All Rights Reserved
Python Functions

#!/usr/bin/python

a = 10

def myFunc():
print "Value of a:”, a;
return

myFunc()

© 2015 Tigran Khazhakyan All Rights Reserved


Problems

1. Find the sum of all the multiples of 3 or 5 below 1000.

2. By considering the terms in the Fibonacci sequence


whose values do not exceed four million, find the sum of
the even-valued terms.

3. What is the largest prime factor of the number 14256

4. A palindromic number reads the same both ways. The


largest palindrome made from the product of two 2-digit
numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two
3-digit numbers.
© 2015 Tigran Khazhakyan All Rights Reserved
Reading from Keyboard

© 2015 Tigran Khazhakyan All Rights Reserved

You might also like