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

Python Licture 1

Uploaded by

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

Python Licture 1

Uploaded by

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

Selected topics 1

Selected Topics 1
Python

Lecture 1
:Prepared by
MSc. Ahmed M. Elnajeh
© 2023
What is Python?

.Python is a popular programming language. It was created by Guido van Rossum, and released in 1991 🔺

:It is used for 🔻


micro controller applications (micro-python scripting) ✔
,web development (server-side) ✔
,software development ✔
,mathematics ✔
AI (Artificial Intelligence) ✔

?Why Python 🔻

.Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, ESP, Arduino, etc) ✔
.Python has a simple syntax similar to the English language ✔
.Python has syntax that allows developers to write programs with fewer lines than some other programming languages ✔

2
🔻 Installing Python 🔻
:Windows 🔺 :Windows 🔺
Download Python from ✔: Download thonny from ✔:
http://www.python.org https://thonny.org/
.Install Python ✔ .Install thonny.exe ✔
Run Idle from the Start Menu known ✔ .Run thonny icon on Desktop ✔
.as python shell

:Remark ⚠
.you can use python on different IDE’s Like visual studio code ide and pycharm ide ✔
.thonny will be used through out this course ✔

3
🔻 Interpreted Languages 🔻
interpreted 🔺
Not compiled like Java ✔
Code is written and then directly executed by an interpreter ✔
Type commands into interpreter and see immediate results ✔

:Java Runtime
Code Compiler Computer
Environment

:Python Code Interpreter Computer

4
The Python Interpreter
Allows you to type commands one-at-a-time and see results 🔺
A great way to explore Python's syntax 🔺
Repeat previous command: Alt+P 🔺

5
Our First Python Program 🎯
Python does not have a main method like Java 🔺
The program's main code is just written directly in the file 🔺
Python statements do not end with semicolons 🔺

Filename, preferred extension is py

hello.py
1 print("Hello, world!")

User Program

6
The print Statement 🎨

print("text")
print() (a blank line)
Escape sequences such as \" are the same as in Java
' Strings can also start/end with

swallows.py
1 print("Hello, world!")
2 print()
3 print("Suppose two swallows \"carry\" it together.")
4 print('African or "European" swallows?')

7
Comments 📝

:Syntax
comment text (one line) #

swallows2.py
1 # Suzy Student, CSE 142, Fall 2097
2 # This program prints important messages.
3 print("Hello, world!")
4 print() # blank line
5 print("Suppose two swallows \"carry\" it together.")
6 print('African or "European" swallows?')

8
💦 Exercise 💦
:Rewrite the Figures lecture program in Python. Its output
______
\ /
\ /
/ \
/______\
/ \
/______\
+--------+
______
\ /
\ /
| STOP |
/ \
/______\
______
\ /
\ /
+--------+
9
Types in Python
int
Bounded integers, e.g. 732 or -5
float
Real numbers, e.g. 3.14 or 2.0
long
Long integers with unlimited precision
str
’Strings, e.g. ‘hello’ or ‘C

10
Basic Operations

:Assignment ⚫
size = 40 ✔
a = b = c = 3 ✔
Numbers ⚫
integer, float ✔
complex numbers: 1j+3, abs(z) ✔
Strings ⚫
'hello world', 'it\'s hot' ✔
"bye world" ✔
"""" continuation via \ or use """ long text ✔

11
⛓ String operations ⛓
concatenate with + or neighbors 🔺
'word = 'Help' + 'x ✔
'word = 'Help' 'a ✔
subscripting of strings 🔺
'Hello'[2]  'l' ✔
'slice: 'Hello'[1:3]  'el 🔺
word[-1]  last character ✔
len(word)  5 ✔
.Strings are immutable: cannot assign to subscript or string can not be changed ✔
For example
Word[0] = '!' error will be occurred because string are
.immutable

12
Lists
Lists can be heterogeneous ⚫
a = ['spam', 'eggs', 100, 1234, 2*2] ✔
:Lists can be indexed and sliced ⚫
a[0]  spam ✔
a[:2]  ['spam', 'eggs'] ✔
Lists can be manipulated ⚫
a[2] = a[2] + 23 ✔
a[0:2] = ['spam', 'eggs'] ✔
][ = a[0:0] ✔
len(a)  5 ✔

13
Basic programming While loop
a,b = 0, 1

:while b < 10
formatted output, without \n #
print(b)
multiple assignment #
a,b = b, a+b

14
Control flow: if Statement
x = int(input("Please enter #:"))
:if x < 0
x = 0
print('Negative changed to zero‘)
:elif x == 0
print('Zero‘)
:elif x == 1
print('Single‘)
:else
print('More‘)
no case statement ⬛
:Remark ⚠
Python 3 uses input to read user input as a string (str) ✔
15
Control flow: for Loop
a = ['cat', 'window', 'defenestrate']
:for x in a
print(x, len(x))
no arithmetic progression, but ◼
range(10)  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ◼
:for i in range(len(a)) ◼
print(i, a[i])
do not modify the sequence being iterated over ◼

16
Loops: break, continue, else

break and continue like C ◼


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

17
Do nothing

pass does nothing ◾


syntactic filler ◾
:while 1
pass

18
Functions ☂

.Function: Equivalent to a static method in Java ⚫


hello2.py
:Syntax ⚫ 1 # Prints a helpful message.
:)(def name 2
3
def hello():
print("Hello, world!")
statement 4
5 # main (calls hello twice)
statement 6 hello()
7 hello()
...
statement

Must be declared above the 'main' code ✔


Statements inside the function must be indented ✔
19
Whitespace Significance
}{ Python uses indentation to indicate blocks, instead of ⚫
Makes the code simpler and more readable ✔
.In Java, indenting is optional. In Python, you must indent ✔

hello3.py
1 # Prints a helpful message.
2 def hello():
3 print("Hello, world!")
4 print("How are you?")
5
6 # main (calls hello twice)
7 hello()
8 hello()

20
Thanks ⌛

Trust your self

Believe in your self

You might also like