learn python the hard way (personal) -- Ex27-

Ex27:

一系列逻辑练习,类似于离散数学中的逻辑部分,所以跳过。

 

Ex28:

仍然是逻辑练习部分

 

Ex29:

if语句

people = 20
cats = 30
dogs = 15

if people < cats:
    print("Too many cats! The world is doomed!")

 

Ex30:

if-else语句:

people = 30
cars = 40
trucks = 15

if cars > people:
    print("We should take the cars.")
elif cars < people:
    print("We should not take the cars.")
else:
    print("We can't decide.")

What happens if multiple elif blocks are True? Python starts at the top and runs the frst block that is
True, so it will run only the frst one.
 

Ex31:

if-else语句的嵌套使用

elif door == "2":
        print("You stare into the endless abyss at Cthulhu's retina.")
        print("1. Blueberries.")
        print("2. Yellow jacket clothespins.")
        print("3. understanding revolvers yelling melodies.")

        insanity = input(">")

        if  insanity == "1" or insanity == "2":
            print("Your body survives powered by a mind of jello.")
            print("Good job!")
        else:
            print("The insanity rots your eyes into a pool of muck.")
            print("Good job!")

How do I tell whether a number is between a range of numbers? You have two options: Use 0 < x <
10 or 1 <= x < 10, which is classic notation, or use x in range(1, 10).
注意是 insanity == "1"

Ex32:

for循环 及 list:

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'diems', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print(f"This is count: {number}")

# same as above
for fruit in fruits:
    print(f"A fruit of type: {fruit}")

# also we can go through mixed lists too
# notice we have to use {} since we don't know what's in it
for i in change:
    print(f"I got {i}")

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0,6):
    print(f"Adding {i} to the list.")
    # append is a function that lists understand
    elements.append(i)

Common Student Questions


How do you make a 2-dimensional (2D) list? That’s a list in a list like this: [[1,2,3],[4,5,6]]


Aren’t lists and arrays the same thing? Depends on the language and the implementation. In classic
terms a list is very different from an array because of how they’re implemented. In Ruby though
they call these arrays. In Python they call them lists. Just call these lists for now since that’s what
Python calls them.


Why is a for-loop able to use a variable that isn’t defined yet? The variable is defned by the for-loop
when it starts, initializing it to the current element of the loop iteration each time through.


Why does for i in range(1, 3): only loop two times instead of three times? The range() function
only does numbers from the frst to the last, not including the last. So it stops at two, not three
in the preceding. This turns out to be the most common way to do this kind of loop.


What does elements.append() do? It simply appends to the end of the list. Open up the Python shell
and try a few examples with a list you make. Any time you run into things like this, always try to
play with them interactively in the Python shell.
 

Ex33:

while循环:

i = 0
numbers = []

while i < 6 :
    print(f"At the top i is {i}")
    numbers.append(i)

    i += 1
    print("Numers now:", numbers)
    print(f"At the bottom i is {i}")

用for循环来实现上面的while循环。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值