Boot camp notes

Python foudation

Varibles,Expressions, and Statements

  1. Varibles Naming Rules: Must start with a letter or underscore _
x, y, z = 12.2, 14, 'Python' 
# the right hand side is actually a tuple. We will learn tuples at a later time.
print(x, y, z)
  1. Arithmetic Operators:
**power
/division
%remiander
//quotient
  1. hashtag#
  2. About the is operator

    is checks whether both the operands refer to the same object or not, i.e., the same id.

    == compares the values only.
a,b=2,2
a is b 
>>False
a == b
>>True

Conditionals

  1. nested conditionals
if...:
	if...:
	
  1. chained conditionals
if...:
elif...:
else:
  1. try/except structure
inp = input('Enter Fahrenheit Temperature: ')

try:
    fahr = float(inp)
    cel = (fahr - 32.0) * 5.0 / 9.0
    print(cel)
except:
    print('Please enter a number')

>>Enter Fahrenheit Temperature: MSBA
>>Please enter a number

Functions

  1. routine
def fuc(a,b):
	return a+b
  1. lambda expression
    one-line; anonymous
    can difine a variable by condition to go on!!
addone = lambda x: x+1
sumup = lambda x, y: x+y
sumup(1,2)
>>3

# conditional output
compare = lambda x: x*2 if x < 10 else x 
compare(12)
>>12
compare(5)
>>10

# directly execute the lambda function
v = (lambda x: x * x)(100)
v
>>10000

#conbined fuc&lambda
my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70]
result = filter(lambda x: (x % 13 == 0), my_list)
print(list(result)) 
  1. map function
# map is used to let one function to run in mutiple objects
abs(-3)
>>3
abs(-3,-5,-7)
>> error
list(map(abs,(-2, 10, -9)))
>> [2,10,9]

# map & def fuc
def addition(n):
    return n + n
  
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
>>[2,4,6,8]

#map & lambda
num1 = [1, 2, 3]
num2 = [4, 5, 6]
result = map(lambda x, y: x + y, num1, num2)
print(list(result))
>>[5,7,9]

Loops and Iteration

  1. continue and break
while True:
    line = input('>>>Enter something: ')
    if line[0] == '#':
        continue     
        # ends the current iteration 
        # jumps to the top of the loop
        # starts the next iteration
    if line == 'done':
        break     
        # jumps out of the entire loop
        
    print(line)
print('The End!')


>>>Enter something: hku
hku
>>>Enter something: # hello
>>>Enter something: ???
???
>>>Enter something: done
The End!
  1. while vs for
    while means condition is met; for means iteration variable
  2. nested for loops
# range print direction —— default:lengthways
for x in range(5):
    for _ in range(x + 1):
        # we use _ as the interation variable
        # we do not need to use its value
        print('*', end = '')
    print()
>>
*
**
***
****
*****
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值