Python for Beginners - Simply understand programming concepts
Macowtdy
7 views
41 slides
Aug 25, 2024
Slide 1 of 41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
About This Presentation
Learn Python with simple exercises
Credits - Freepik
Size: 1.3 MB
Language: en
Added: Aug 25, 2024
Slides: 41 pages
Slide Content
Programming
with Python
ONLINE
HELLO, PYTHON!
Why Python?
For what?
Why is it special?
What can we do?
One of the most simple ,
less complex languages
Easy to learn
Much faster execution
Web technology, Image
processing, Graphics , AI &
ML
LET'S GRAB PYTHON
Go to : https://www.python.org/
DO IT WITH ME :)
Run Python Shell??
PLAY AROUND WITH PYTHON SHELL
Basic math : + , - , * , /
Decimal Points : .01 .0002
Powers (indices)
Variables
VARIABLES
The word variable in programming describes a place to store
information such as numbers, text, lists of numbers and text,
and so on.
CASE SENSITIVE
name =/= Name =/= nAMe
name = 'Roy'
Name = 'Joy'
nAmE = 12
RULES
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different
variables)
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
2myvar = "John"
my-var = "John"
my var = "John"
Create a variable called 'age'
Assign a value to it.
age = 13
Simple as that.
Create a variable called 'name'
Put your name to it.
name = "Dilina"
Create a variable called 'isRaining'
Assign a value to it.
isRaining = True
Create a variable called 'price'
Assign a value to it.
price = 20.57
1 2
3 4
DATA TYPES
Although python is not a typed language, it is good to have an
understanding on data types.
Text Type: str (String)
Numeric Types: int (Integers), float (Decimals)
Sequence Types: list (Lists, Arrays)
Mapping Type: dict (Dictionary)
Boolean Type: bool (1 or 0 - True or False)
HELLO WORLD
Create a new file called hello.py
Write print("Hello World!")
Save it.
Run it.
ACTIVITY
Check results from following codes
a = 10
b = 20.0
c = a+b
print(c)
What happens after executing above code?
And why?
a = 'Peter'
b = 'James'
c = a+b
print(c)
a = 'Peter'
b = 2
c = a+b
print(c)
QUESTION
A pen costs $10.
A book costs $24
A pencil costs $14
Peter buys 10 books,23 pencils and 100 pens.
How much should peter pay in total?
Write a simple program called total.py to print the
output. (The total)
STRINGS
NO CALCULATIONS.
BUT A LOT OF TEXT.
text_s = ' Learn Python, He said! '
text_d = " Learn Python, He said! "
text_m = " Learn Python, He said! '
text_o = " I'm Learning python "
text_oo = ' I'm Learning python '
STRINGS
myNum = 5
myNum = "5"
Math operations
- Add, Sub,Mul,Div etc.
String operations
-Replace,Concat,Capitalize etc.
STRINGS > ARRAYS?
myName = "Dilina John"
Letter - Index
D -> 0
i -> 1
l -> 2
i -> 3
n -> 4
a -> 5
Length?
Indexes?
0 - 5
6
STRINGS CONT
Letter - Index
D -> 0
i -> 1
l -> 2
i -> 3
n -> 4
a -> 5
myName[0]
myName = "Dilina"
myName[4]
myName[2:4]
myName[:4]
myName[1:]
myName[-2]
CHANGE TYPES?
WE CALL IT CASTING
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
string <-> float <-> int
BOOLEANS
True or False? 1 or 0?
We also use booleans in Conditional
blocks , Loops etc.
print(10 > 9)
print(10 == 9)
print(10 < 9)
Example -
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
PYTHON OPERATORS
Arithmetic Operators
Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
PYTHON OPERATORS
Comparison & Logical
Comparison
<
>
<=
>=
!=
==
Logical
and
or
not
Try it -
a = True and False
b = (1==1) or (1>2)
c = not ( 5!=6 and 2>1 )
print(a)
print(b)
print(c)
CONDITIONAL
BLOCKS
RUN THIS AND SEE?
Any errors?
a = 33
b = 200
if (b > a):
print("b is greater than a")
CONDITIONAL
BLOCKS
RUN THIS AND SEE?
How about now?
a = 33
b = 200
if (b > a):
print("b is greater than a")
INDENTATION
TAB SPACE is required, Defines Scope
a = 200
b = 33
if (b > a):
print("b is greater than a")
else:
print("a is greater than b")
CONTD. IF ELSE
Multiple conditions??
a = 200
b = 33
if (b > a):
print("b is greater than a")
elif (a == b):
print("a and b are equal")
else:
print("a is greater than b")
WATCH OUT FOR
TEMPERATURE
Write a simple program to print "It is Hot" if
temperature > 40 else print "It is Cold".
Initial temperature would be 75.
Hint : Use a variable called temperature.
ASKING FOR INPUT
Ask for some input from the user
myName = input(" Enter your name? ")
print(myName)
RUN THIS AND SEE?
HOT OR COLD?
ASK USER FOR THE TEMPERATURE
DISPLAY "It is HOT" if temperature > 40
DISPLAY "It is COLD" otherwise.
POSITIVE OR
NEGETIVE ?
ASK USER FOR A NUMBER AND PRINT
WHETHER IT IS NEGETIVE OR POSITIVE?
EXTENTION :
ALSO CHECK IF IT IS ZERO
SCIENCE MARKS
Ask user for their science marks?
IF MARKS > 75
PRINT "A"
MARKS > 65
PRINT "B"
MARKS > 55
PRINT "C"
ELSE
PRINT "F"
ITERATION/LOOPS
Use them to repeat a task
Ex - Printing numbers from 1 to 1000.
for loop while loop
for i in range(0,8):
print(i)
for i in range(2,8):
print(i)
for i in range(8):
print(i)
i = 0
while i < 8:
print(i)
i += 1
i = 2
while i < 8:
print(i)
i += 1
FOR LOOP CONT.
QUESTION 01
PRINT NUMBERS FROM 10 - 10000
QUESTION 02
PRINT "HI" 1000 times
QUESTION 03
TAKE THE NAME FROM THE USER
PRINT "HELLO" + NAME 100 times.
QUESTION 04
PRINT NUMBERS FROM 100 to 1
NESTED FOR LOOPS
More than one for loop inside another
Syntax:
for i in range(0,10):
print("Inside first loop")
for y in range(0,10):
print("Inside second loop")
WHAT IS THE
OUTPUT?
for i in range(0,5):
print("Hi")
for x in range(0,4):
print("Hello")
print("Bye")
WHILE LOOP
i = 0
while i < 8:
print(i)
i = i + 1
Ideal use : When the number of iterations is
not known in prior.
Uses a seperate variable to control the loop.
WHILE LOOP CONT.
QUESTION 01
PRINT NUMBERS FROM 10 - 10000
QUESTION 02
PRINT "HI" 1000 times
QUESTION 03
TAKE THE NAME FROM THE USER
PRINT "HELLO" + NAME 100 times.
QUESTION 04
PRINT NUMBERS FROM 100 to 1
PATTERS USING FOR LOOP
HINT : USE TWO FOR LOOPS ONE INSIDE OTHER.
Print a line break (new line) -> print(" ")
Printing in the same line -> print( " * ",end = ' ' )
LISTS / ARRAYS CONT.
NEW LIST
mylist = ["apple", "banana", "cherry", "avacado"]
Adding elements to list
mylist.append("orange")
print(mylist)
Remove elements from list
mylist.remove("apple")
print(mylist)
BREAK & CONTINUE STATEMENTS
BREAK
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
CONTINUE
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)