【跟着MIT学Python】Unit 1.2 Core Elements of programs

Unit 1.2 Core Elements of programs

  • Exercise
in: str1 = ‘hello’
in: str[-1]
out: 'o'
in: str4 = 'helloworld'
in: str4[::-1]
out: 'dlrowolleh'

When calling a string, s[a:b], character in (a,b-1) is included except s[b]

INPUT/OUTPUT

print

in: x = 1
in: x_str = str(x)
in: print("my fav num is",x, ",", "x = ",x)
out: my fav num is 1 , x = 1
# the int type automaticly add a space in front and at the end of the variable
in: print("my fav num is"+x_str+","+"x = "+x_str)
out: my fav num is1,x = 1
# those would stick together

input

Input simply expects everything to be a string.

in: text = input("type something")
out: type something # waiting for something to be typed in
in: "foo"
in: text
out: '"foo"'
in: print(5*text)
out: "foo""foo""foo""foo""foo"

What if I want to type a number into it?

in: number = int(input("type something"))

IDEs

Control Flow

Branching programs

if

if <condition>
<expectation>
elif <condition>
<expectation>
else <condition>
<expectation>

while

while(<condition>):
<expectation>
<expectation>
...

for

for n in range(5): # range(n) gives a sequence of integers between 0 and 5
print(n)

out:
0
1
2
3
4

range(start,end,step)

for i in range(7,10):
mysum +=i
print(mysum)
#mysum = 7+8+9
for i in range(5,11,2):
mysum +=i
print(mysum)
#mysum = 5+7+9
for i in range(n): #every time goes into range(n), i will increment itself once.
<arguement>

break

break myself out of the loop if we wanna quit in the half way

for i in range(5,11,2):
mysum += i
if mysum >=5
break
print(mysum)

out: 5

Exercise

num = 10
while True
	if num < 7:
		print('Breaking out of loop')
		break
	print(num)
	num -= 1
print('Outside of loop')

out:
10
9
8
7
Breakinf out of loop
#Here 'break' directly bring pointer outside the while loop....
Outside of loop

选了感兴趣的machine learning但是课程的前置要求是python所以在edx上找了MIT的网课开始看。
因为有C的基础所以感觉不是很难。老师上课条理清晰节奏适中,感觉是小白也能轻松入门的课程。
记录学习新语言的每一天~

点下面的课程链接可以跳到对应的网课地址:
Introduction to Computer Science and Programming Using Python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值