So,today is the continue of CS50 Python. Today's assigment are about the 1)data type and theri notices. 2)self define funtion.3)function with return.
When finish today's python class,I have renew my knowleadge that there are lots of details are different between C and Python, although someone always say : python is basic on C,you know C you can directly do python program. If you trust them without thinking, you must have losts of block when go python program by C brain.
Why could I say that? is that cause Chinese don't cheat Chinese? hahhaLOL. Here is the evidence of little small things about basic data type.
Thus it's very nessasery for me to learning python once time,may be not so serious at the first time learning, but At least I should go though once.
Under these, are the code I typed all of thes 3 themes I have write. It's the most basic things that without :loop, if, array, structure(if python has),heap memory manage......and may be more fresh things I never see before.I'm very eager to try and familiar with them.
First Source Code:
#SS2:Calculator string int float
#2.1 Bulid a calculator
# 1)input function will atom take the typed as string
# 2)convert syntax: CONVERTTYPE() EX: int(.....) double(.....)
x = int(input("What is number"))
y = int(input("What is number"))
z = x+ y
print(z)
# 3)you could also do ⬆️ these in one sentence ⬇️:
print( int( input("What is x?") ) + int( input("What is y?") ) )
# 4)round the result to the nearest possible integer,the syntax:round(number[,ndight])
# 4.1))When you see square brakets like this [ ], this means you are about to see
# something optional.
# 4.2)In the prameter the number means the value you want to around, the ,ndight means
# the more specify number of digits that you want the round function to round to.
# RE:docs.python.org/3/library/functions.html#round
x = float(input("What is float x?"))
y = float(input("What is float y?"))
z = round(x + y,2)#means save the 2 set after point
print(z)
# 5)automatically formatted number by three zero
print(f"{z:,}")
# 6)Divide in python default is point number
z = x / y
print(z)
z = round(x / y,2)
print(z)
# 7)specify using an f string how many digits you want to print
print(f"{z:.2f}")# ⇔ print(round(x,2))
Second Source Code:
🌟Attention: Python don't have something like C to decleare the origin of function above main() function. it only should make function all decleare above the enterence function nor you can't processing calling other functions.
#SS3:Def function
#3.02 If you a function, it must exist by the time you are calling it(not same as C,this main isn't ture enterance)
# 1)In python you want to put the main part of your code at the top of your file then can down the define of other functions.
# 2)Remember at the last you must call main
def main():
hello()
name = inut("What is your name?")
hello(name)
#3.01 If and when you define your own functions you can using def keywoeds
# 1)syntax:def 'name'():
# 2)The : means every line of code I indent underneath this one as the menaing of this new function
def hello():
print("hello")
name = input("What is your name? ").strip().title()
hello()
print(name)
#3.01 Perameterize the function ⇔ function with prameter
# 1)If you want the value of parameter by default
def helloWithP(to="world"): #hello with default parameter
print("hello " + to)
helloWithP()
name = input("What is your name?")
helloWithP(name)
main()#3.02.2)
#3.02 Scope refer to a variable only existing in the context in which your defined it
Third Source Code:
def main():
x = int(input("what is x?"))
print("x squared is",square(x))
def square(n):
return n ** 2# or return power(n,2) or return n*n
main()