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

Python Basics Week 1

The document provides an overview of Python fundamentals including number types, arithmetic expressions, variables, strings, lists, and control flow statements. It discusses integers and floats, operators and precedence, variables, strings, lists, if/else conditional statements, and the Boolean data type.

Uploaded by

Quang Thang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Basics Week 1

The document provides an overview of Python fundamentals including number types, arithmetic expressions, variables, strings, lists, and control flow statements. It discusses integers and floats, operators and precedence, variables, strings, lists, if/else conditional statements, and the Boolean data type.

Uploaded by

Quang Thang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

RESOURCES

Some great resources to start your work :

• Fundamentals of Python: First Programs


• https://docs.python.org/3/contents.html
• https://python.swaroopch.com/
NUMBER TYPES

Number - Two types

int (integer): whole number


10, 20, 100
float (floating): fractional number with decimal points
10.5, 2.5, 100.5

Example:
print (3 + 4) # integer addition - output 7
print (3.5 + 4.5) # float addition - output 8.0
print(13 / 2) # / division operator - output 6.5
print(13 // 2) # floor operator - discards the decimal part – output 6
print(13 % 2) # remainder (modulo) operator - returns the remainder
of the division – output 1
ARITHMETIC EXPRESSION

• An arithmetic expression consists of operands and operators


combined in a manner that is already familiar to you from
learning algebra

Fundamentals of Python:
First Programs
PRECEDENCE RULES

Precedence rules:
• ** has the highest precedence and is evaluated first
• Unary negation is evaluated next
• *, /, and % are evaluated before + and -
• + and - are evaluated before =
• With two exceptions, operations of equal precedence are
left associative, so they are evaluated from left to right
• ** and = are right associative
• You can use () to change the order of evaluation

Fundamentals of Python:
First Programs
EXPRESSION EVALUATION

Fundamentals of Python:
First Programs
VARIABLES

To store information or data and then work with them later, you need
to know exact location of the memory where data is stored.

Variables are used as a pointer to the memory location where you


store variable information.

Example:

width=10 # The equal sign ( = ) is used to assign a value to a variable


height=5
total=width*height
print(total) # output 50

Go to Week 1 PDF_CODE
STRING

String - String is a sequence of character enclosed in single


quotes ('...') or double quotes ("...")

Example:
print (" My First Program")
My First Program

print ("My First Program \n My First Class") # \n – special


character used for new – line

My First Program
My First Class

Go to Week 1 PDF_CODE
LISTS

Lists - It is a series of same type data. Data can be


numbers or characters.

Example 1
array1 = [ 1, 3, 5, 7, 9]
print(array1) # output- [1, 3, 5, 7, 9]

print(array1[0]) # array1 with index 0 –output 1


print(array1[1])
print(array1[-1])
print(array1[-4:]) # slicing returns a new list [3, 5, 7, 9]
Go to Week 1 PDF_CODE
CONTROL FLOW STATEMENT

So far the statements in Python were executed in top-down


order. But sometimes it is important to change the flow of
work depending on the situations.

Example -You might need to take decisions (buy or not buy)


based on the price of the product. This might be
achieved using control flow statements

Control flow statements - if , for and while (3 conditions)


SELECTION: if AND if-else STATEMENTS

Selection statements allow a computer to make


choices
• Based on a condition

The if statement is used to check whether a condition is true or


false:
• if the condition is true, we run a block of statements (called the
if-block), else we process another block of statements (called
the else-block).
• The else clause is optional.
• Works with Boolean data type
BOOLEAN DATA TYPE ( O OR 1)

• Boolean data type consists of two values: true and false


(typically through standard True/False)

• Example: 5 = 4 evaluates to False

Fundamentals of Python:
11
First Programs
CONTROL FLOW STATEMENT - IF

Go to Week 1 PDF_CODE
Thank You
No tutorial in this week

You might also like