十分钟内学会 Python

#
#  main.py
#  Some Python Program
#
#  Created by Mewlan Musajan on 4/27/21.
#  Excerpt From: Stochastic Technologies “Tutorial - Learn Python in 10 minutes.” Stavros Korokithakis.                                                 https://www.stavros.io/tutorials/python/
#

print(range(10))
rangeList = list(range(10))
print(rangeList)

for number in range(10):
    if number in (3, 4, 7, 9):
        print("number is in it")
        break
    else:
        continue
else:
    pass
    
if rangeList[1] == 2:
    print("the second item is 2")
elif rangeList[1] == 3:
    print("the second item is 3")
else:
    print("Donno")

while rangeList[1] == 1:
    print("We are trapped in an infinite loop!")
    break

funcvar = lambda x: x + 1
print(funcvar(1))

def passingExample(someList, someInt, someString = "A default string"):
    someList.append("a new item")
    someInt = 4
    return someList, someInt, someString

someList = [1, 2, 3]
someInt = 10
print(passingExample(someList, someInt))

class SomeClass(object):
        common = 10
        someVariable = 0
        def __init__(self):
            self.someVariable = 3
        def someFunction(self, someArgument, anotherArgument):
            return self.someVariable

someClass = SomeClass()
someClass.someFunction(1, 2)

anotherClass = SomeClass
print(anotherClass.common)

SomeClass.common = 30


class AnotherClass(SomeClass):
    def __init__(self, someArgumentment):
        self.someVariable = 3
        print(someArgumentment)
    def someMethod(self, someInt, someString):
        someInt += 1
        someString += " world"
        if someInt == 1:
            someInt = "One day,"
        else:
            someInt = "%s days passed," % someInt
        return "%s I said %s to myself." % (someInt, someString)

someAnotherClass = AnotherClass("hello")
print(someAnotherClass.someVariable)
print(someAnotherClass.someMethod(1, "hello"))

def someFunction():
    try:
        10 / 0
    except ZeroDivisionError:
        print("Oops, zero can't be a dividend.")
    else:
        pass
    finally:
        print("We're done with that.")

someFunction()


import random


print(random.randint(1, 100))

import pickle

someList = ["This", "is", 4, 13327]
someFile = open(r"binary.dat", "wb")
pickle.dump(someList, someFile)
someFile.close()

someFile = open(r"text.txt", "w")
someFile.write("This is a sample string.")
someFile.close()

someFile = open(r"binary.dat", "rb")
loadedlist = pickle.load(someFile)
someFile.close()
print(loadedlist)

list1 = [1, 2, 3]
list2 = [3, 4, 5]
print([x * y for x in list1 for y in list2])
[3, 4, 5, 6, 8, 10, 9, 12, 15]
print([x for x in list1 if 4 > x > 1])

if any([i % 3 for i in [3, 3, 4, 4, 3]]):
    print("one of the number is divisible with 3 in the list provide.")
    
print(sum(True for i in [3, 3, 4, 4, 3] if i == 4))

del list1[0]
print(list1)
del list1

number = 5

def someFunction():
    print(number)

def yetAnotherFuncion():
    global number
    number = 3

someFunction()
yetAnotherFuncion()
print(number)







 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值