mosh python

Alt text

Alt text

expression : a statement get a result

primitive type

boolean only -> False True

string

1
2
3
4
5
6
7
message = """
dasf
as dg
adgs
asdg asdf
adsf
"""
1
2
str1 = "hello"
str1[0]
1
2
3
4
5
first = "Mosh"
last = "Hamedani"
full = f"{first + last}"

print(full)
1
2
3
4
5
str1 = "Hello world"

str1.find("lo")
if "lo" in course:
pass
1
2
3
4
abs(-2.9)

import math
math.ceil(2.2)

falsy -> bool()
“”
0
None


ternary operator

1
2
3
4
5
6
7
8
age = 22
# if age >= 18:
# message = "Eligible"
# else:
# message = "Not eligible"

message = "Eligible" if age >= 18 else "Not eligible"
print(message)

short circule

1
2
if high_income and good_credit and not student:
pass

如果high_income 是FALSE,good_credit 根本没有进行判断

“b” > “a” -> True

iterable

for x in “hello”
pass

function default return None


class level attribute

1
2
3
4
5
6
7
8
9
10
11


class Point:
default_color = "red"
def __init__(self):
pass

point = Point()
print(point.default_color)
print(Point.default_color)

class method

1
2
3
4
5
6
7
8
9
10
class Point:
def __init__(self, x, y):
pass

@classmethod
def zero(cls):
return cls(0, 0) # Point(0, 0)
pass

point = Point.zero()
1
2
3
4
5
6
7
8
9
10
11
class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __str__(self):
return f"({self.x}, {self.y})"

point = Point(1, 2)
print(point)
print(str(point))