活動地址:CSDN21天學習挑戰賽
1,學習目標
提示:可以添加學習目標
例如: 一周掌握 Python 入門知識
2,學習內容
提示:可以添加要學的內容
例如:
A,掌握 Python 基本語法
B,掌握 Python 列表、字典、Usage of collection types such as tuples
C,掌握 Python 函數
D,掌握 Python 常用模塊
E, 掌握 Python 模塊與包
F,掌握 Python 類與對象
3,學習時間
Take out two hours a day
4,學習產出
CSDN技術博客 每天一篇
# 案例1
# x = 10
# y = 20
# print(10 + 20)
# print(x + y)
# print(x - y)
# print(x * y)
# print(x / y)
# 案例2
# x = 10
# print(id(x))
# x = 100
# print(id(x))
# 變量命名規范
a1 = 10 # 不能是1a
x = 10
X = 100
# 姓名 = "yuan"
# print(姓名)
# 見名知意
age = 20
firstName = "lin"
secondName = "hu"
first_name = "li"
# 一 關於注釋
# The content of the comment is not to bepythonWhat the interpreter executes,For people to make annotations 這是一個單行注釋
''' 這是一個多行注釋! Use three single quotes or three double quotes to distinguish '''
# 二 About statement splitting symbols
# x = 10;y = 20 # Semicolons are supported as delimiters,但是不推薦
a = 1
b = 2 # A newline is recommended as the delimiter
# 三 關於縮進 流程控制語句
# 四 pep8規范
c = 20 # control+alt+l : Formatting shortcut
def foo():
pass
def bar():
pass
x = 100
# python內置函數:type: View the type of a data
print(type(x)) # <class 'int'>
f = 3.1415926
print(type(f)) # <class 'float'>
a = 3.14E3 # Exponential expression: E3即 10的三次方
print(a)
# 布爾類型 : True False
print(2 == 3) # The return value is a boolean value
print(2 < 3) # True
print("yuan" == "yuan") # True
print(type(1 > 2)) # <class 'bool'>
print("1" == 1)
# 零值: There is a boolean for each data typeFalse的值
# 整型的零值:0 字符串的零值:"" The zero value of a list is the empty list The zero value of a dictionary is an empty dictionary
# 內置函數: bool
print(bool(1))
print(bool(100))
print(bool(-1))
print(bool(0))
print(bool("False"))
print(bool("0"))
print(bool(""))
print(bool([]))
print(bool({
}))
x = 10
b = True
# 一 創建一個字符串
s = "hello world" # Strings are marked with double or single quotes
print(s)
# 二 字符串的轉義符 \: 1. Convert some common symbols to special symbols ,比如\n 2. Convert some special symbols to normal symbols
s2 = 'i\'m yuan! \ni\'m a teacher!'
print(s2)
path = "E:\s32\day02\\next"
print(path)
# 三 長字符串
s3 = " 杜甫 \n白日依山盡 \n黃河入海流"
print(s3)
s4 = ''' 杜甫 白日依山盡 黃河入海流 '''
s5 = """ 請選擇你的操作 1 購買商品 2 結算 3 退出 """
print(s5)
# 四 字符串的格式化輸入
name = "rain"
age = 23
gender = "男"
# The so-called template or format string: Embed a dynamically changing value somewhere in a string.
# s6 = "姓名 yuan.年齡 22.性別 男."
# 占位符號 %s:Take place for the string %d:Places place for decimal integer numbers %f: Place placeholders for floating point types
s6 = "姓名:%s.年齡:%d.性別:%s" % (name, age, gender)
# s6 = "姓名:rain.年齡:23.性別:男"
print(s6)
s = "hello world!"
# A string is composed of several characters in sequence
# Indexing operations on sequence types: 通過索引獲取值 (支持正負索引) 語法格式 數據對象[索引]
print(s[4])
print(s[6])
print(s[-7])
print(s[-1])
# Slicing operations on sequence types:切片操作 獲取多個值 語法格式 數據對象[開始索引:結束索引:step=1]
# 特點1 左閉右開 顧頭不顧尾
# 特點2 :The step size defaults to 1,That is, cutting one by one from left to right
print(s[0:5])
print(s[6:10])
print(s[6:11])
print(s[6:]) # The default to the right of the colon means that it is taken to the end
print(s[:5]) # The left side of the colon defaults to the secondary index0開始
print(s[:]) # 完整切片
print("s[5:0]:::", s[5:0]) # Match from left to right
print(s[-5:]) # orld!
print(s[-6:-1]) # world
print(s[-3:-5]) # 結果為空
# The step size parameter is positive:Represents cutting from left to right Cut from right to left for negative numbers
# 步長為 -1 : Cut one by one from right to left
print(":::", s[0:5:1])
print(":::", s[0:5:-1])
print(":::", s[4::-1]) # olleh
print(":::", s[::-1]) # !dlrow olleh
# 步長為 2: Take one every other from left to right
print(":::", s[::2])
# Sequence type judgment operation:in 判斷 返回一個布爾值
print("yuan" in "hello yuan")
print("yuan" not in "hello yuan")
print("rain" in "hello yuan")
print("yua" in "hello yuan")
print("yuan hello" in "hello yuan")
# Concatenation of sequence types: 拼接 使用 +
s1 = "hello "
s2 = " yuan"
print(s1 + s2)
# 補充
print("*"*100)
# 內置方法: 數據對象.方法()
# 字符串內置方法: String data object.方法()
# ******************************************** upper方法 lower方法 ********************************************
# 案例1
s = "HELLO"
s_upper = s.upper()
print(s_upper)
print(s)
# 案例2:變量的重新賦值
# s = "hello"
# s = s.upper()
# ******************************************** startswith endswith ********************************************
s1 = "hello yuan"
# ret = s1.startswith("hello") # 返回一個布爾值
ret = s1.endswith("uan") # 返回一個布爾值
print(ret, type(ret)) # True <class 'bool'>
# ******************************************** isdigit: 判斷是否是數字字符串 ****************************************
ret1 = 10 + 20
ret2 = "10" + "20"
print(ret1, type(ret1)) # 30 int
print(ret2, type(ret2)) # "1020" str
print("hello".isdigit()) # False
print("100".isdigit()) # True
print("100個".isdigit()) # False
# ******************************************** strip方法:去除兩端空格 ************************************************
s2 = " yuan "
ret3 = s2.strip() # 去除兩端空格 ”yuan“
print(ret3)
print(ret3 == "yuan")
# ******************************************** 字符串分割:split 字符串拼接join ****************************************
s = "北京,上海,深圳,廣州" # []
ret4 = s.split(",") # Convert a string to a list
print(ret4) # ['北京', '上海', '深圳', '廣州'] ----> "北京;上海;深圳;廣州"
ret5 = "---".join(ret4)
print(ret5) # 北京---上海---深圳---廣州
# 計算運算符
x = 9
print(10 / 3)
print(10 % 4)
print(x % 2 == 0)
# 比較運算符 > < == >= <=
print(2 == "2") # False
print(2 != "2") # True
print(2 > 5) # False
# 賦值運算符 = +=
a = 10
a += 1 # a = a + 1 # 自加一
print(a)
# 邏輯運算符 與運算 and 或運算:or 非運算 not
user = "yuan"
pwd = 123
print(user == "yuan" and pwd == 123) # True
# print("OK")
# name = "yuan"
# age = 22
# print(name)
# print(name,age,sep=",")
# print(111,end=";")
# print(222)
# 輸入函數 inputAccepted must be a string
# name = input("請輸入一個姓名:") # To use the user to enter a value at the terminal
# age = input("請輸入一個年齡:") # To use the user to enter a value at the terminal
# print("姓名:%s 年齡:%s" % (name, age))
# 案例2
a = input("num1>>>") # "20"
b = input("num2>>>") # "30"
# print(int(a)) # Convert a numeric string to an integer
print(int(a) + int(b)) # Convert a numeric string to an integer
# 順序執行
# print("OK")
# print("OK")
# print("OK")
# 分支語句之if語句
''' 在python中用:+Indentation indicates the body of a statement # 單分支語句 if 表達式: 語句1 語句2 語句3 # 雙分支語句 if 表達式: 語句1 語句2 語句3 else: 語句1 語句2 語句3 # 多分支語句 if 表達式1: 語句1 語句2 語句3 elif 表達式2: 語句1 語句2 語句3 elif 表達式3: 語句1 語句2 語句3 ... else: 語句1 語句2 語句3 '''
# 案例1
# if 2 > 1:
# print("ok1")
# print("ok2")
# print("ok3")
# 案例2: 單分支語句
# user = "rain"
# pwd = 123
#
# if user == "yuan" and pwd == 123:
# print("登錄成功!")
#
# print("程序結束!")
# 案例3:雙分支語句
# user = input("請輸入用戶名:")
# pwd = input("請輸入密碼:") # ”123“
#
# if user == "yuan" and pwd == "123":
# print("登錄成功!")
# else:
# print("用戶名或者密碼錯誤")
#
# print("程序結束!")
# 案例4:判斷成績
# score = int(input(">>>"))
#
# if score > 100 or score < 0:
# print("非法輸入")
# elif score >= 90:
# print("成績優秀")
# elif score >= 80:
# print("成績良好")
# elif score >= 60:
# print("成績及格")
# else:
# print("成績不及格")
# 案例5
content = input(">>>")
# 判斷是否是數字字符串
if content.isdigit():
score = int(content)
# judging results
if score > 100 or score < 0:
print("非法輸入")
elif score >= 90:
print("成績優秀")
elif score >= 80:
print("成績良好")
elif score >= 60:
print("成績及格")
else:
print("成績不及格")
else:
print("請輸入一個數字")
# while:條件循環 for:遍歷循環
''' while 表達式: 循環語句體 語句 '''
# 案例1:無限循環
# while 1:
# # print("OK")
# score = int(input(">>>"))
#
# if score > 100 or score < 0:
# print("非法輸入")
# elif score >= 90:
# print("成績優秀")
# elif score >= 80:
# print("成績良好")
# elif score >= 60:
# print("成績及格")
# else:
# print("成績不及格")
# print("程序結束!")
# 案例2:循環打印1-100
# count = 0 # 初始變量
# while count < 100: # 判斷表達式
# # print("OK")
# count += 1 # count = count+1 步進語句
# print(count) # 循環語句體
#
# print("程序結束!")
# 案例3: 計算1+2+3+...+100
# count = 0 # 初始變量
# ret = 0
# while count < 100: # 判斷表達式
# # print("OK")
# count += 1 # count = count+1 步進語句
# # print(count) # 循環語句體
# ret += count # ret = ret+count
#
# print(ret)
# 案例4:打印1-100中所有的偶數
# count = 0 # 初始變量
# ret = 0
# while count < 100: # 判斷表達式
# # print("OK")
# count += 1 # count = count+1 步進語句
# if count % 2 == 0:
# # count是一個偶數
# print(count)
# 案例5:
count = 0 # 初始變量
ret = 0
while count < 100: # 判斷表達式
# print("OK")
count += 1 # count = count+1 步進語句
print(count)
if count == 88:
break # 退出整個while循環
The program randomly builds a number within a certain range as a guess,由用戶猜測此數字.用戶每猜測一次,由系統提示猜測結果:太大了、Too small or guessed right,Until the user guesses the result correctly or the number of guesses runs out, it fails.
設定一個理想數字比如:66,
讓用戶三次機會猜數字,如果比66大,則顯示猜測的結果大了;
如果比66小,則顯示猜測的結果小了;
只有等於66,顯示猜測結果正確,退出循環.
最多三次都沒有猜測正確,退出循環,並顯示‘都沒猜對,繼續努力’.
cnt = 0
realNum = random.randint(0,10) # randint(0,10)的邊界值, 左右邊界的0和10都是可以取到的.
realNum = 66
while cnt < 3:
num = int(input("請輸入您猜的數字:"))
cnt += 1
if num < realNum:
print("猜小了")
elif num > realNum:
print("猜大了")
else:
print("猜對了!")
break
if cnt == 3:
print("三次機會已經用完!")