之前paddlepaddle VIT的教学课程
单独拿出来记录一下 哈哈
一开始都是一些比较基础的内容 感兴趣的可以看一下
原文:Python入门
print("The n makes a new line") The makes a new line
print("The t is a tab") The is a tab
print('I'm going to the movies') I'm going to the movies
**# Using to not accidently close the string by having a closing "** print("This is a string enclosed by "" not '' ") This is a string enclosed by "" not ''
**# Creating a variable** **# Variables are used to store information to be referenced** **# and manipulated in a computer program.** firstVariable = 'Hello World' print(firstVariable)
**# go over ? mark after if you are not sure what method does.** print(firstVariable.lower()) print(firstVariable.upper()) print(firstVariable.title()) hello world HELLO WORLD Hello World
**# use help to look up what each method does** help(firstVariable.lower) Help on built-in function lower: lower(...) S.lower() -> string Return a copy of the string S converted to lowercase.
firstVariable.split(' ') ['Hello', 'World']
a=firstVariable.split(' ') print(a) ['Hello', 'World']
' '.join(a) 'Hello World'
print("0" + "1") 01
"0" * 3 '000'
**# You can also add strings together.** "Fizz" + "Buzz" 'FizzBuzz'
#基础数学: **+ - * /** **# Exponentiation **** **# This operator raises the number to its left to the power of the number to its right** 2**3 8
**# Modulo** **# Returns the remainder of the division of the number to the left by the** **# number on its right.** 9%3 0
#if 语句 **# Notice you have to indent after you start a if statement.** num = 3 if num == 3: print(num) 3
**# Nothing is outputted because num > 10 is FALSE** num = 3 if num > 10: print(num)
num = 3 if num % 3 == 0: print("Fizz") Fizz
num = 10 if num % 5 == 0: print("Buzz") Buzz
**#检查是否为True,如果是,则执行此 *** 作。如果它不是True(False),则不执行** if True: print("This was True") This was True
if False: print("Nothing printed")
**#逻辑 *** 作符** num = 4 num > 0 and num < 15 True
**# both the conditions are true, so the num will be printed out** if num > 0 and num < 15: print(num) 4
**# num > 0 is True, num > 15 is False** **# Since the first condition is True, it is True** num = 4 num > 0 or num > 15 True
if num > 0 or num > 15: print(num) 4
**# or will only evaluate to False if both are False** if False or False: print('Nothing will print out')
num = 10 not num < 20 False
**#else 语句** **#必须在if或elif语句之后。最多可以有一个其他声明。仅当上面的所有“if”和“elif”语句都为False时才会执行** num = 1 if num > 3 : print("Hi")
**#We will execute what is inside the else statement because num is not greater than 3** num = 1 if num > 3 : print("Hi") else: print("number is not greater than 3") number is not greater than 3
**#We will execute what is inside the if statement because num > 4** num = 4 if num > 3 : print("Hi") else: print("number is not greater than 3") Hi
ep:将num分配给整数值。 如果整数是偶数,写一个if else组合将打印“你的整数是偶数”。否则,打印“你的整数是奇数”。 提示:任何可以精确地除以2的整数都是偶数(例如:2,4,6)。 任何不能精确地除以2的整数都是奇数(例如:1,3,5)。 使用模运算符(%),它将数字左边的余数除以右边的数字。 num = 3 if num % 2 == 0: print("Your integer is even") else: print("Your integer is odd") Your integer is odd
**elif 语句:** 必须在if语句之后。 elif语句语句允许您检查True的多个表达式,并在其中一个条件求值为True时立即执行代码块。 与else类似,elif语句是可选的。但是,与其他情况不同,最多只能有一个语句,if后面可以有任意数量的elif语句。
num = 21 if num > 50: print('num is larger than 50') elif num == 21: print('num = 21') else: print('Catchall condition') num = 21
my_num = 5 if my_num % 2 == 0: print("Your number is even") elif my_num % 2 != 0: print("Your number is odd") else: print("Are you sure your number is an integer?") Your number is odd
**# You can have mulitple elif statements.** **# Remember only the first True statement has its block of code executed.** dice_value = 1 if dice_value == 1: print('You rolled a {}. Great job!'.format(dice_value)) elif dice_value == 2: print('You rolled a {}. Great job!'.format(dice_value)) elif dice_value == 3: print('You rolled a {}. Great job!'.format(dice_value)) elif dice_value == 4: print('You rolled a {}. Great job!'.format(dice_value)) elif dice_value == 5: print('You rolled a {}. Great job!'.format(dice_value)) elif dice_value == 6: print('You rolled a {}. Great job!'.format(dice_value)) else: print('None of the conditions above (if elif) were evaluated as True') You rolled a 1. Great job!
ep:将num分配给整数值。 编写一系列if,elif,else语句,打印您指定的num。 但是对三的倍数要打印“Fizz”而不是数字, 五的倍数要打印“Buzz”。 对于三和五共同的倍数则打印“FizzBuzz” if num % 3 == 0 and num % 5 == 0: print('FizzBuzz') elif num % 3 == 0: print('Fizz') elif num % 5 == 0: print('Buzz') else: print(str(num))
#列表 # Defining a list z = [3, 7, 4, 2]
#访问列表里面的值 # The first element of a list is at index 0 z[0] 3 z[2] 4
z[-2] 4
#切分列表 **# first index is inclusive (before the:) and last (after the:) is not.** **# not including index 2** z[0:2] [3, 7]
# everything up to index 3 z[:3] [3, 7, 4]
# index 1 to end of list z[1:] [7, 4, 2]
#取列表的最大值, 最小值, 长度, 以及总和
print(min(z), max(z), len(z), sum(z)) 2 7 4 16
#对列表中对象出现次数进行统计
random_list = [4, 1, 5, 4, 10, 4] random_list.count(4) 3
#返回列表第一个指针
random_list.index(4) 0
# you can specify where you start your search random_list.index(4, 3) 3
# random_list.index(value, [start, stop]) random_list.index(4, 5, 6) 5
#对列表进行排序 x = [3, 7, 2, 11, 8, 10, 4] y = ['Steve', 'Rachel', 'Michael', 'Adam', 'Monica', 'Jessica', 'Lester'] # Sorting and Altering original list # low to high** x.sort() print(x) [2, 3, 4, 7, 8, 10, 11]
# Sorting and Altering original list # high to low x.sort(reverse = True) print(x) [11, 10, 8, 7, 4, 3, 2]
# Sorting and Altering original list # A-Z y.sort() print(y) ['Adam', 'Jessica', 'Lester', 'Michael', 'Monica', 'Rachel', 'Steve']
# Sorting and Altering original list # Z-A y.sort(reverse = True) print(y) ['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']
# sorting list WITHOUT altering original list new_list = sorted(y) new_list ['Adam', 'Jessica', 'Lester', 'Michael', 'Monica', 'Rachel', 'Steve']
# notice y is unchanged y ['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']
#在列表结尾添加一个对象 x [11, 10, 8, 7, 4, 3, 2] x.append(3) print(x) [11, 10, 8, 7, 4, 3, 2, 3]
#删除列表中一个对象 x.remove(10) print(x) [11, 8, 7, 4, 3, 2, 3]
#删除列表中指定位置的对象 # List before you remove an item print(x) [11, 8, 7, 4, 3, 2, 3]
# Remove item at the index # this function will also return the item you removed from the list # Default is the last index x.pop(3) 4 print(x) [11, 8, 7, 3, 2, 3]
#合并列表 #通过在末尾续加的方式来延长列表 x.extend([4, 5]) x [11, 8, 7, 3, 2, 3, 4, 5]
# lists can be diverse, each element in the list can be of a different type. # lists are really list of pointers, and these pointers can # point to anything. **# Concatenating Lists** print('x+y=',x+y) x+y= [11, 8, 7, 3, 2, 3, 4, 5, 'Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']
#在列表指定位置前插入对象 《前》 x [11, 8, 7, 3, 2, 3, 4, 5] x.insert(4, [4, 5]) x [11, 8, 7, 3, [4, 5], 2, 3, 4, 5]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)