0% found this document useful (0 votes)
18 views13 pages

Pythonday1 - 20 - 09 - Jupyter Notebook

The document is a Jupyter Notebook that covers basic Python programming concepts, including variables, data types, operators, conditional statements, and functions. It provides examples of how to create and manipulate different data types such as integers, floats, strings, and booleans, as well as how to use arithmetic and comparison operators. Additionally, it demonstrates the use of functions to encapsulate code for reuse and includes built-in functions for common tasks.

Uploaded by

phannhu0310
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)
18 views13 pages

Pythonday1 - 20 - 09 - Jupyter Notebook

The document is a Jupyter Notebook that covers basic Python programming concepts, including variables, data types, operators, conditional statements, and functions. It provides examples of how to create and manipulate different data types such as integers, floats, strings, and booleans, as well as how to use arithmetic and comparison operators. Additionally, it demonstrates the use of functions to encapsulate code for reuse and includes built-in functions for common tasks.

Uploaded by

phannhu0310
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

9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

Variables and Data types


In [3]:

x = 5
#create value 5 and assign value to x before executing output by ctrl+enter

In [5]:

Out[5]:

In [1]:

x = 5
print (x)

In [6]:

y = 8

In [7]:

Out[7]:

In [11]:

a,b = (4,3)
#create multiple variables

In [19]:

Out[19]:

In [20]:

Out[20]:

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 1/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [22]:

print (b)
#same function to show a variable as assign directly its name as b

Data types

Insert cell
(## is used for heading 2 in markdown cell)

In [16]:

type (x)
#checking type function then Python will reccognize type of x as integer

Out[16]:

int

In [26]:

z=9.
#with . Python recgonize as float point as 0.9 or 1.3 plapla

In [27]:

Out[27]:

9.0

In [28]:

type (z)

Out[28]:

float

In [34]:

u = True
v = False
#withBoolenremembercapitalisefirstletter

In [32]:

print (u)

True

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 2/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [35]:

print (v)

False

In [36]:

type(u)

Out[36]:

bool

String
In [37]:

x = "VietDuc"
#sigle or double quotation work well on Python

In [38]:

print(x)

VietDuc

In [39]:

type(x)

Out[39]:

str

In [40]:

name = 'Phan Nhu'

In [41]:

print(name)

Phan Nhu

In [46]:

"I'm fine"

Out[46]:

"I'm fine"

In [48]:

y = "I'm fine"

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 3/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [49]:

print (y)

I'm fine

In [44]:

'I\'m fine'

Out[44]:

"I'm fine"

In [45]:

'Today is "Tuesday"'

Out[45]:

'Today is "Tuesday"'

Data types conversion


In [50]:

x = 5
type (x)

Out[50]:

int

In [51]:

x = float (x)
print(x)
type(x)

5.0

Out[51]:

float

In [52]:

y =2.3
type (y)

Out[52]:

float

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 4/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [53]:

y = int(y)
print(y)
type(y)

Out[53]:

int

In [ ]:

# we can convert an integer into string but not string to integer

In [56]:

str(5)

Out[56]:

'5'

In [57]:

print('Big', 'Houses')

Big Houses

In [58]:

print('Big'+' Houses')

Big Houses

Basic Python Syntax

Arithmetic Operators
+, -, *, /, %, , *, ==

In [59]:

2+3

Out[59]:

In [60]:

3-5

Out[60]:

-2

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 5/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [61]:

7*6

Out[61]:

42

In [62]:

15/3

Out[62]:

5.0

In [64]:

17%5
#% return remainder

Out[64]:

In [67]:

6**2
#**take power

Out[67]:

36

In [68]:

5 == 6

Out[68]:

False

In [70]:

#We can use line continuation with \


((3+5)*7)**\
2

Out[70]:

3136

Comparison Operators
==, <, >, <=, >=, !=

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 6/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [71]:

5 > 7

Out[71]:

False

In [72]:

5 != 6

Out[72]:

True

Logical Operators

In [73]:

True and True

Out[73]:

True

In [74]:

(5>3) and (3<=4)

Out[74]:

True

Indexing Strings

In [75]:

x= 'Hello'

In [77]:

print(x)

Hello

In [78]:

print(x[1])
#position 1 of Hello is e, 0 is H

Conditional Statements

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 7/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [84]:

if 5 == 15/3:
print("Horray!")
print("This is my first conditional statement")

Horray!
This is my first conditional statement

In [90]:

x=5
if x>3:
print("x is higher than 3")
else:
print ("x is smaller or than 3")
#if with condition is true

x is higher than 3

In [92]:

x=2
if x>3:
print("x is higher than 3")
else:
if x<0:
print ("x is negative")
else:
print ("x is smaller or equal to 3 but not negative")
#if with condition is false

x is smaller or equal to 3 but not negative

In [7]:

def compare_to_three (x):


if x>3:
return "larger than 3"
elif x<3:
return "smaller than 3"
else:
return "equal to 3"
print(compare_to_three (2))

smaller than 3

In [93]:

#Similarly we have alternative way is elif


x=2
if x>3:
print("x is higher than 3")
elif x<0:
print ("x is negative")
else:
print ("x is smaller or equal to 3 but not negative")

x is smaller or equal to 3 but not negative

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 8/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [94]:

Sep="Python"
Oct="Accounting", "Seminar"
Nov="Audit"
Dec="Security"

In [103]:

this_month = "Sep"
if this_month == "Sep":
print ("Python")
#remember that one equal is for assign, double equal sign is for comparation

Python

In [106]:

this_month = "Oct"
if this_month == "Sep":
print ("Python")
elif this_month == "Oct":
print("Accounting,"+"Seminar")
elif this_month == "Nov":
print("Audit")
else: print("Security")

Accounting,Seminar

Functions
In [107]:

#Function used:group some computation, to re-use it again and again

In [12]:

def plus_ten(a):
return a+10
print(plus_ten(2))

12

In [13]:

plus_ten(3)
#the value of 2, 3, 5 we give and them 'arguments'

Out[13]:

13

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 9/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [11]:

plus_ten(5)

Out[11]:

15

In [114]:

def double_then_square(x):
output = x*2
output = output**2
return output

In [116]:

print(double_then_square(3))
print(double_then_square(2))

36
16

In [117]:

#Faster way
def double_then_square(x):
output = (x*2)**2
return output

In [118]:

print(double_then_square(3))

36

In [14]:

#Fastest way
def double_then_square(x):
return (x*2)**2

In [15]:

double_then_square(3)

Out[15]:

36

In [20]:

def wage(w_hours):
return w_hours*25
def with_bonus(w_hours):
return wage(w_hours) + 50
print (with_bonus(60))

1550

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 10/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [128]:

def times_two(x):
return x*2
print (times_two(5))

10

In [133]:

#method 1
def double_then_square_compose(x):
return times_two(x)**2
print (double_then_square_compose(5))

100

In [127]:

#method 2
def double_then_square_compose(x):
return (x*2)**2
double_then_square_compose(5)

Out[127]:

100

New quick exerxise

In [146]:

def rank(x):
if x>=80:
return("A")
elif x >=60:
return ("B")
elif x >= 60:
return ("C")
elif x >=40:
return ("D")
else: return("E")
print (rank(85))
print (rank(75))
print (rank(58))

A
B
D

Some important Built-in functions in Python

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 11/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

In [147]:

max(10,5,14)

Out[147]:

14

In [148]:

min(10,5,14)

Out[148]:

In [149]:

list=[10,5,14] #define a list by square bracket


print(sum(list))

29

In [150]:

round(3.4567,2)
#round(so, don vi lam tron)

Out[150]:

3.46

In [151]:

round(3.4567)

Out[151]:

In [152]:

len('Python for Finance')


#len used to count number of elements

Out[152]:

18

In [154]:

len(list)

Out[154]:

In [ ]:

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 12/13
9/25/22, 12:37 PM pythonday1_20_09 - Jupyter Notebook

localhost:8888/notebooks/Documents/Python/pythonday1_20_09.ipynb 13/13

You might also like