0% found this document useful (0 votes)
3 views82 pages

Lecture 02 - Python Programming Language 1

The document provides an introduction to Python programming, highlighting its ease of use and popularity. It covers installation steps, basic operations, data types, and the structure of Python programs, including variables, assignments, and conditional statements. Additionally, it explains the concept of objects, values, and types in Python, along with examples of expressions and control flow using conditional statements.

Uploaded by

jbhfvr64v7
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)
3 views82 pages

Lecture 02 - Python Programming Language 1

The document provides an introduction to Python programming, highlighting its ease of use and popularity. It covers installation steps, basic operations, data types, and the structure of Python programs, including variables, assignments, and conditional statements. Additionally, it explains the concept of objects, values, and types in Python, along with examples of expressions and control flow using conditional statements.

Uploaded by

jbhfvr64v7
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

Computational Thinking

and Artificial Intelligence


02. Python Programming Language 1

Hanul Kim
Dept. of Applied Artificial Intelligence
hukim@[Link]
Computational Thinking & 1. Python Programming Language
Artificial Intelligence 2. Object, Value, and Types
Lecture 02, 2025-Fall 3. Conditional Statements
Why Python?
Why Python: Easy

# include <stdio.h>

int main(void)
{
printf(“Hello, world!\n”);
return 0;
}
C Programming Language

VS
print(“Hello, world!”)
Python Programming Language
[Link]

Why Python: Popular


[Link]
system-2023/python/
How to install Python?
How to install Python?

1. Install Python interpreter


2. Install Visual Studio (VS) Code
3. Install VS Code Python extension
4. Select Python interprester
Install
Python
Interpreter
Install
VS Code
Install
Python
Extension
Select
Python
Interpreter

Window: ctrl+shift+p
Mac: command+shift+p
Select
Python
Interpreter

Window: ctrl+shift+p
Mac: command+shift+p
Python as a Calculator
Run Python in Interactive Mode

>> python
Open terminal
or
and run Python
>> python3
Run Python in Interactive Mode

Or open terminal in
VSCode (ctrl + `)
and run Python
Run Python in Interactive Mode

This is Python
interactive mode
Python interpreter
will execute your
Python code
Run Python in Interactive Mode

>>> 2 + 5
Run Python in Interactive Mode

>>> 2 + 5
7
>>>

Python interpreter will


execute your Python code
Run Python in Interactive Mode

Q) Run the below Python codes:


1. >>> 2 * 3
2. >>> 2 + 3 * 2
3. >>> (2 + 3) * 2
4. >>> 7 / 2
5. >>> 7 // 2
6. >>> 2 ** 3
7. >>> -3.1 + 2 * 2.3
8. >>> (-3.1 + 2) * 2.3
Jupyter Notebook for Python Interactive Mode

§ Jupyter notebook gives


the best way for Python
programming with
interactive mode

1. Install Jupyter notebook


extension for VS Code
2. Open .ipynb file
(interactive python
notebook file)
Jupyter Notebook for Python Interactive Mode

§ Set Python interpreter for


Juyper notebook
Jupyter Notebook for Python Interactive Mode

§ Set Python interpreter for


Juyper notebook
Jupyter Notebook for Python Interactive Mode

Cell 1

§ Jupyter notebook allow


Cell 2
you to write and run Python
code cell-by-cell
Jupyter Notebook for Python Interactive Mode

§ Jupyter notebook allow


you to write and run
Python code cell-by-cell
The Classic First Program
“Hello, World!”
Run Python in Script Mode

Create
Python
script
Run Python in Script Mode

Write
Python script has
print(‘hello, world’)
‘.py’ extension Python
program
Run Python in Script Mode

Write
Python script has
print(‘hello, world’)
‘.py’ extension Python
program
Run Python in Script Mode

Run
Python
program
Run Python in Script Mode

Run
Python
program
Run Python in Script Mode

Run Python interpreter with Run


the Python script “[Link]”
Python
program
Computational Thinking & 1. Python Programming Language
Artificial Intelligence 2. Object, Value, and Types
Lecture 02, 2025-Fall 3. Conditional Statements
How does Python support primitive data and functions?
Objects, Values, and Types

Objects
§ Python program manipulate objects
§ Objects have an identity (ID), a value, and a type:
• An identity (ID) is a number to distinguish an object*
• A value is a data, which we should manipulate
• A type defines the kinds of operations (functions) a program can perform

* In Python, identity refers to the memory address where an object is located.


Objects, Values, and Types

Types
§ A type defines the kinds of operations (functions) a program can perform
§ Python support primitive types to represent diverse primitive data
• There is no value: None
• Number: int (integer number), float (real number)
• Logical value: bool (True or False)
• String: string
• ...
The value of object: 4

The ID of object: 436018344

The type of object: int


Objects, Values, and Types

Scalar and non-scalar objects


§ Objects are scalar or non-scalar
• Scalar object cannot be subdivided
• Non-scalar objects have internal structure that can be accessed

1
True
a b c d
1.0 None

Sclar object Non-sclar object


Example: Number Types

int float
§ A type for integer number object § A type for real number object
• 1 • 1.42153
• -3 • -3.202415
• 0 • 0.364123
• 10 • 10.214215164069046
• 2 • 2.1204159601245061
• ... • ...
Example: Number Types

Arithmetic operators for number types

i + j addition
i – j subtraction
i * j multiplication
i // j floor division
i / j division
i % j remainder
i ** j power
Expressions

Expressions
§ Objects and operators can be combined to form expression
§ Each expression can be evaluated to an object of some type. This is called the value
of the expression

>>> 3 + 7
>>> 3 + 7*(2+3)
Examples of expressions
>>> 3 + 7*2+3
>>> 3
Variables and Assignment

Variable
§ In Python, a variable is just a name, nothing more (it is important!)

Assignment
§ An assignment operator = associate the name to left with the object

variable = expression

name assignment operator object


Variables and Assignment
Variables and Assignment

id value

size 4357645000 5
Variables and Assignment

id value

size 4357645000 5

4357645096 8
Variables and Assignment

Example: Variables and assignment

pi = 3
radius = 11
area = pi * (radius**2)
radius = 14

1. This code first binds the name pi and radius to different objects or type int
2. It then binds the name area to a thrid object of type int
3. If the program the excute radius = 14, the name radius is rebound to a different
object of type int
Variables and Assignment

Example: Variables and assignment


Variables and Assignment

Variable names
§ Variable names can contain:
• letters
• digits
• the underscore character _
§ Variable names cannot start with a number
Variables and Assignment

Reserved names (keywords)


§ A few reserved names built-in meanings in Python
§ So, these names cannot be used as variable names

and break elif for in not True

as class else from is or try

assert continue except global lambda pass while

async def False if nonlocal raise with

await del finally import None return yield


Statements

Assignment statements
§ A single line of Python code including assignment operator is called to an assignment
statement:

variable = expression

What is a statement?
Statements

Statements
§ A statement is a comend (single line of code) that instructs the computer to perform a
specific action or task, such as declaring a vraible, assigning a value, or executing a
function

statement

pi = 3 statement
radius = 11
area = pi * (radius**2) statement
radius = 14

statement
Statements

Multi-line statements
§ Python provides two ways to write long statements across multiple lines.
Python Program

Python program (Python script)


§ A python program consists of statements
§ Python interpretor executes each statement one by one, from top to bottom,
this is called, sequential execution

1st statement

pi = 3 2nd statement
radius = 11
area = pi * (radius**2) 3rd statement
radius = 14

4th statement
Python Program

Comment
§ A comment is a piece of text in the code that is ignored by the Python interpreter
§ Comments are used to explain code, make it more readable, and provide context or
notes for future reference
Python Program

Types of comments in Python


§ Python supports two types of comments:
• Single-line comments (using #)
• multi-line comments (using triple quotes """ """ or ''' ''')

"""
This is a multi-line comment.
It can span multiple lines.
Python ignores this text.
"""
x = 10 # Code continues here
Python Program

Can we change the execution order of statements (control flow)?


§ Programming language provides tools to change control flow:
• Conditional statements
• Loop statements
• Call statements
Computational Thinking & 1. Python Programming Language
Artificial Intelligence 2. Object, Value, and Types
Lecture 02, 2025-Fall 3. Conditional Statements
Boolean Objects

Example: Comperative operators for number types

i == j i and j equal?
i != j i and j are not equal?
i < j i is less than j?
i <= j i is less than or equal to j?
i > j i is greater than j?
i >= j i is greater than or equal to j?

What type of object will an expression containing comparison operators evaluate to?
Boolean Objects

bool
§ A type to represent logical values: True or False
§ The results of comparertive operators are bool objects
Boolean Objects

is and is not
§ is and is not operators compare the IDs of objects (is the same objects?)

1 == 1 VS 1 is 1
Boolean Objects

Logical operators

a and b True if both a and b are True, otherwise False


a or b True if either a or b is True, otherwise False
not a False if a is True, True if a is False
Boolean Objects

Example) Predict the value of the following expressions


1. True and not False
2. True and not false
3. True or True and False
4. not True or not False
5. True and not 0
6. 52 < 52.3
7. 1 + 52 < 52.3
8. 4 != 4.0
Branching A Program

Conditional Statements
§ Typically, conditional statements have three parts
• Test: evaluate expression (return logical value)
• True code block: a group of code that is
executed when the test value is True
• False code block: a group of code that is
executed when the test value is False
Branching A Program

Code block
§ A code block is a group of statements that are guaranteed to be executed as a unit.

if A:
B
C
D A B C D E F
else:
E Code block1 Code block2
F
Branching A Program

Indentation: build a code block in Python


§ In Python, the : symbol indicates the start of a block of code
§ In Python, you can gorup statements into a code block by using indentiation
§ In Python, indentation is done by inserting four spaces (recommended style)

Note: Many programming languages, including C/C++, use curly brackets {} to group statements into a code block.
Branching A Program

Python conditional statements

if Boolean expression: if Boolean expression:


code block code block
elif Boolean expression:
code block
if Boolean expression:
else:
code block
code block
else:
code block
Python Program with Simple Conditional Statements

num = int(input(“Enter a number: ”))


if num == 0:
print(“Your number is 0”)
elif num > 0:
print(“Your number is positive”)
else:
print(“Your number is negative”)

Enter a number:
Python Program with Simple Conditional Statements

num = int(input(“Enter a number: ”))


if num == 0:
print(“Your number is 0”)
elif num > 0:
print(“Your number is positive”)
else:
print(“Your number is negative”)

num 5

Enter a number: 5
Python Program with Simple Conditional Statements

num = int(input(“Enter a number: ”))


if num == 0:
print(“Your number is 0”)
elif num > 0:
print(“Your number is positive”)
else:
print(“Your number is negative”)

num 5

Enter a number: 5
Python Program with Simple Conditional Statements

num = int(input(“Enter a number: ”))


if num == 0:
print(“Your number is 0”)
elif num > 0:
print(“Your number is positive”)
else:
print(“Your number is negative”)

num 5

Enter a number: 5
Python Program with Simple Conditional Statements

num = int(input(“Enter a number: ”))


if num == 0:
print(“Your number is 0”)
elif num > 0:
print(“Your number is positive”)
else:
print(“Your number is negative”)

num 5

Enter a number: 5
Your number is positive
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 n 1
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 n 1 a 2
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 n 1 a 2 b 3
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 z 0 n 1 a 2 b 3 z 0
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0
True z = 0
True
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 z 0 n 1 a 2 b 3 z 0
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: False if n > 0: False
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 z 0 n 1 a 2 b 3 z 0
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 z 0 n 1 a 2 b 3 z 0

0
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 z 3 n 1 a 2 b 3 z 0

0
Python Program with Complex Conditional Statements

n = 1 n = 1
a = 2 a = 2
b = 3 b = 3
z = 0 z = 0
if n > 0: if n > 0:
if a > b: if a > b:
z = a z = a
else: else:
z = b z = b
print(z) print(z)

n 1 a 2 b 3 z 3 n 1 a 2 b 3 z 0

3 0
Thank You

You might also like