[go: up one dir, main page]

0% found this document useful (0 votes)
1 views15 pages

Week1_Lecture2

The document provides an introduction to Python programming, covering basic syntax, variables, and data types. It includes examples of integer, float, string, and boolean operations, as well as string functions and De Morgan's Laws. The content is aimed at beginners and serves as a foundational guide for understanding Python and its applications in AI.
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)
1 views15 pages

Week1_Lecture2

The document provides an introduction to Python programming, covering basic syntax, variables, and data types. It includes examples of integer, float, string, and boolean operations, as well as string functions and De Morgan's Laws. The content is aimed at beginners and serves as a foundational guide for understanding Python and its applications in AI.
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/ 15

Introduction to Python and AI

Week 1: Introduction to
Python and Programming
Basics

Instructor: Zaid Almahmoud


AIM Lab Postdoctoral Scholar
Introduction to Python and AI

Lecture 2:
Basic Syntax and
Variables
Variables and Data Types
 Variable: A container that stores information.
 Variables Stores Data (number, word…etc).
 In Python:

name = "Alice"
age = 25
temperature = 37.2
 Common Data Types
 Strings (text): "Hello, World!"
 Integers (whole numbers): 25
 Floats (decimals): 3.14
 Booleans (True/False): True
Variables and Data Types

 Media Example: Film database application

movie_title = "Inception"
release_year = 2010 Python
Code
rating = 8.8
Database
Movie Title Release Year Rating
Inception 2010 8.8
Integer Operations
Python code:
1 counter = 1
2 counter += 1 Increment by 1
3 counter += 4
------------------------------------------------------------

Q: What is the value of counter after executing line 2?


A: 2

Q: What is the value of counter after executing line 3?


A: 6
Integer Operations
Python code:
1 speed = 20
2 speed *= 2 Multiply by 2
3 speed *= speed
------------------------------------------------------------

Q: What is the value of speed after executing line 2?


A: 40

Q: What is the value of speed after executing line 3?


A: 1600
Integer Operations
Python code:
1 x = 8
2 y = 5
3 z = x + y
4 z = z % 2
------------------------------------------------------------

Q: What is the value of z after executing line 3?


A: 13

Q: What is the value of z after executing line 4?


A: 1
Float Operations
Python code:
1 x = 8.5
2 y = 4.5
3 z = x + y
4 z = z / 2
5 z = int(z) Convert z to
integer
------------------------------------------------------------

Q: What is the value of z after executing line 3?


A: 13
Q: What is the value of z after executing line 4?
A: 6.5
Q: What is the value of z after executing line 5?
A: 6
String Operations
Python code:
Comment (not part of the
message = "Hello World" program)

Indexing first_letter = message [0] # "H"

Indexing sixth_letter = message [5] # " "


Concatenati
on
message_with_name = message + " Hamad" # "Hello World Hamad"
Slicing +
Concatenati no_space_message = message[0:5] + message[6:] # "HelloWorld"
on
Indexing +
Concatenati first_and_last = message[0] + message [-1] # "Hd"
on
Conversion
+ message_with_number = message + " " + str(1) # "Hello World
Concatenati
on
1"
message_with_integer = message + " " + 1 # ERROR: can’t
concatenate string with integer
String Functions
Python code:

message = " Hello World! "


message = message.strip() # "Hello World!"

message = message.replace("!", ".") # "Hello World."


message = message.lower() # "hello world."
message = message.capitalize() # "Hello world."
message = message.title() # "Hello World."
message = message.upper() # "HELLO WORLD."
message = message.swapcase() # "hello world."
String Functions
Python code:

message = "Hello World!"

index= message.find("o") # 4 (finds the index of the first


occurrence)
index= message.find("Hello") # 0 (finds the index of the first
occurrence)
index= message.find("Python") # -1 (returns -1 if no such substring
found)
index= message.index("o", 5) # 7 (returns the index of the first
occurrence starting from a given index)
index= message.index("Python") # Raises ValueError: substring not found
Boolean Operations
Python code:
TV_ON = True
Radio_ON = False
TV_ON_check = TV_ON # True
all_ON_check = TV_ON and Radio_ON # False
all_ON_check = TV_ON == True and Radio_ON == True # False
all_OFF_check = not TV_ON and not Radio_ON # False
all_OFF_check = TV_ON == False and Radio_ON == False # False
one_ON_check = (TV_ON and not Radio_ON) or (not TV_ON and Radio_ON) #
True
Radio_not_ON_check = not Radio_ON # True
Boolean Operations
Python code:
x = 2
y = 3
x == 2 # True
x == 2 and y == 4 # False
x == 2 or y == 4 # True
x > 0 and y >= 0 # True
x < -1 or y > 0 # True
0 < x < 10 # True
x < y < 10 # True
Boolean Operations
De Morgan’s Laws:
De Morgan's Laws describe how negation distributes over logical
disjunction (OR) and conjunction (AND) :
1) not (x or y) = not x and not y
2) not (x and y) = not x or not y
Examples:
1) “Today is not (Friday or Saturday)” is equivalent to “Today is not
Friday and not Saturday.”
2) “She’s not both (a photographer and a reporter)” is equivalent to
“She’s not a photographer or she’s not a reporter.”
References
[1] Learn Python. “Variables and Types”. Learn Python. Available from:
https://www.learnpython.org/en/Variables_and_Types

[2] “De Morgan's laws”. Wikipedia. Available from:


https://en.wikipedia.org/wiki/De_Morgan%27s_laws

You might also like