Professional Documents
Culture Documents
“Python”
Python
• Python is a clear and powerful object-oriented programming OOP language, comparable
to Perl, Ruby, Scheme, or Java.
• Python is a general-purpose programming language that is often applied in scripting roles.
Reasons for using python:
✓ Less application development time: Python code is usually 2–10 times shorter than comparable
code written in languages like C/C++ and Java, which means that you spend less time writing your
application and more time using it.
✓ Ease of reading: A programming language is like any other language — you need to be able to read
it to understand what it does
✓ Reduced learning time: The creators of Python wanted to make a programming language with
fewer odd rules that make the language hard to learn
• Books:
• Object-Oriented Programing With Python
• Beginning Programming with Python® For Dummies by John Wiley & Sons, Inc
Downloading & Install Python
• Python itself may be fetched from the downloads page on the website,
http://www.python.org/download. Choose the version according to
platform (windows – linux – mac - ..)
Install with Windows
• The name of this file varies, but normally it appears as python-
3.3.4.msi for 32-bit systems and python-3.3.4.amd64.msi
• Double click on the file the next to finish
Accessing Python
A Windows installation creates a new folder in the Start menu that contains
Python installation. You can access it by choosing Start➪All
Programs➪Python 3.10.0 ➪ IDLE(Python 3.10.0).
The Prompt
>>>
waits for your commands
❑ Print
• Print (‘Hassan ‘s book’) error
• Print (“Hassan ‘s book”) true
• Print (‘ this book titled “python” ’) true
String type str
>>> a = “Book”
>>>Print(a) Book
>>> a = [1, 2, 3]
>>> b = [4, 5]
>>> a + b
[1, 2, 3, 4, 5]
>>> b * 3
[4, 5, 4, 5, 4, 5]
Sorting Lists
The sort method sorts a list in place.
>>> a = [2, 10, 4, 3, 7]
>>> a.sort()
>>> a
[2, 3, 4, 7 10]
List as a matrix
>>>L=[[2,3],[4,5],[6,7]]
>>>L[1] [4,5]
>>>L[1][1] 5
L[0][1] 3
Dictionaries
• Mapping Operations :When written as literals, dictionaries are coded in curly braces and
consist of a series of “key: value” pairs. Dictionaries are useful anytime we need to
associate a set of values
• with keys—to describe the properties of something, for instance. As an example,
consider the following three-item dictionary (with keys “food,” “quantity,” and “color”):
>>> D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
>>>D = {‘name': ‘ali', ‘age': 41, ‘work': ‘Lec'}
>>> rec = {'name': {'first': 'Bob', 'last': 'Smith'}
,'job': ['dev', 'mgr'], 'age': 40.5}
>>> print(D['name']) ali
>>>D[‘name’].append(‘ahmed’)
D[‘age’].append(‘30’)
d[‘work’].append(‘eng’)
Tuples
• like a list that cannot be changed
>>>T = (1, 2, 3, 4)
>>>len(T) 4
>>>T[0] 1
• Why Tuples?
• So, why have a type that is like a list, but supports fewer operations?
Frankly, tuples are not generally used as often as lists in practice, but their
immutability is the whole point. If you pass a collection of objects around
your program as a list, it can be changed anywhere; if you use a tuple, it
cannot. That is, tuples provide a sort of integrity constraint that is
convenient in programs larger than those we’ll write here
numbers
+, -, /,*, %, **
>>>print(2**3) 8
>>>print(3%2) 1
== equal , !=not equal , >=, <=
>>> 7 + 2
9
>>> 7 - 2
5
>>> 7 * 2
14
>>> 7 / 2
3
>>> 7 ** 2 7**3??
49
>>> 7 % 2
1
>>>import math
>>> math.pi
3.1415926535897931
>>> math.sqrt(85)
9.2195444572928871
The output:
******
******
******
******
This program that will ask the user for 10
numbers and then computes their average.
>>>s = 0
>>>for i in range(10):
>>>num = eval(input('Enter a number: '))
>>>s = s + num
>>>print('The average is', s/10)
This program will add up the numbers from 1
to 100
>>>s = 0
>>>for i in range(1,101):
>>>s = s + i
>>>print('The sum is', s)
Write a program to square some numbers
>>> s = []
>>> for x in [1, 2, 3, 4, 5]: # This is what a list comprehension does
s.append(x ** 2) # Both run the iteration protocol internally
>>> print(s) #print the list
OUTPUT
[1, 4, 9, 16, 25]
examples
>>>for x in range(2, 30, 3):
>>>print(x)
for x in range(6):
print(x)
else:
print("Finally finished!")
>>>for x in range(6):
>>>print(x)
Output:
0
1
2
3
4
5
While eample
>>>i = 1
>>>while i < 6:
>>> print(i)
>>> i += 1
1
2
3
4
5
while
>>> x = 4
>>> while x > 0:
print('spam!' * x)
x -= 1
OUTPUT
spam!spam!spam!spam!
spam!spam!spam!
spam!spam!
spam!
>>>thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
>>>print(thislist[2:5])
Output:
[cherry", "orange", "kiwi“]