生活随笔
收集整理的這篇文章主要介紹了
基本运算符与if while详解:
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
'''
基本運算符與if while詳解:
'''
# 算術(shù)運算符
# + - * / % // ** # 返回一個數(shù)值# 比較運算符
# > >= < <= == != # 返回一個布爾值# 賦值運算符
# =
x = 10# 邏輯運算符(把多個條件同時疊加)name = 'nick'
height = 180
weight = 140# # and 左右兩個條件都為True,則為True,否則為False
# print(name == 'nick' and height == 180) # True
# print(name == 'nick1' and height == 180) # False
#
# # or 左右兩個條件只要有一個滿足則為True,否則為False
# print(name == 'nick' or height == 190) # True
# print(name == 'nick1' or height == 190) # False
#
# # not 否,如果條件為True,則為False,如果條件為False,則為True
# print(not name == 'nick') # False# 身份運算符
# 每一個變量值都有內(nèi)存地址(身份)
x= 257
y = x
z= 257print(id(x) == (id(y)))
print(x is y) # is比較的是內(nèi)存地址
print(x is not y) # is not判斷是否不等于
print(not x is y)
print(id(x) == id(z))
print(x is z)# 位運算符(從來沒見過)# 60 13 十進制 0,1,2,3,4,5,6,7,8,9,10# 0,1,2,3,4,5,6,7,8,9,逢十進一位,10,11,12,13,...19,20...90,91,92,..99,100# 0和1 二進制# 0,1,逢二進一位,10,11,100,101,111,1000# 0 # 0000 0000 --》0
# 1 # 0000 0001 --》 1
# 10 # 0000 0010 --》 2
# 11 # 0000 0011 --》 3
# 100 # 0000 0100 --》 4
# 101 --》 5
# 110 --> 6
# 111 --> 7# 0100 0011 -->
# 方法一,計算器:67
# 方法二:手工計算# 9892 == 2*10**0 + 9*10**1 + 8*10**2 + 9*10**3
print(2*10**0 + 9*10**1 + 8*10**2 + 9*10**3)
# 01000011 == 1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0
print(1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0)'''
a = 0011 1100 # 60b = 0000 1101 # 130000 1100 # 12# 科學計算(計算原子彈的彈道軌跡)的情況下'''# 0000 a
# 0001 b
# 0010 c# 0110 e# 0001 0010 bc# 成員運算符:判斷元素是否在容器類元素里面(字符串)
class_student_lt = ['s1','s2','s3']
print('s1' in class_student_lt) # True
print('s1' not in class_student_lt) # False
print('s4' in class_student_lt) # Falses = 'nick'
print('n' in 'nick')# Python運算符優(yōu)先級
# + - * / : 先算* / 再算 + -,就叫做優(yōu)先級# 需要優(yōu)先,就加括號,括號優(yōu)先級最高 (經(jīng)驗) --》 頓悟,上課的聽的萌萌懂,print((1+2)*3)# # print(1.2 - 1.0 == 0.2) # True
# # # 010101010 01010101010
# # # 010101010 - 01010101010 # 一些差錯
# # print(round(1.2 - 1.0)) # 浮點數(shù)運算時造成的不確定尾數(shù)
# #
# #
# # # 12--》 0101010
# # # 1.2 --》 101001100101010101010100100101
# # # 1.0 --》 101010110011101100110101001010
# # # 101010111111111110000000000000
# #
# #
# # # 流程控制 --> 控制 變量變化 的一個方向
# #
# # # IPO --> input process output
# #
# # # x = 10
# # # for i in range(10):
# # # print(i*10)
# # #
# # #
# # # x = 10
# # # name = input('')
# # # if name == 'nick':
# # # print(x*10)
# # # else:
# # # print(x*100)
# #
# #
# # x = 10
# # x = x + 10
# # print(x)
# # x += 10 # x = x + 10 # +=:賦值運算符
# # '''
# # -= : x = x -
# # *= : x = x *
# #
# # '''
# # print(x)
#
#
# # if判斷 if(如果)---》判斷
#
#
# # # 單分支結(jié)構(gòu)
# # name = 'nick'
# # inp = input('name: ')
# # if name == inp:
# # print('擼起袖子加油干')
# # print(1)
#
# '''
# if 條件: (:表示你接下來的代碼需要縮進) # 條件為True運行縮進內(nèi)代碼;不成立不運行縮進內(nèi)代碼
# print('擼起袖子加油干')
# code1
# code2
# code3
# code3
# 代碼塊
#
# print(1)
# '''
#
# # 雙分支結(jié)構(gòu)
#
# '''
# if 條件:
# code1 條件成立執(zhí)行code1
# else:
# code2 條件不成立執(zhí)行code2
# '''
# # name = 'bzr'
# # s = input('name: ')
# # if s == name:
# # print('猥瑣的班主任')
# # else:
# # print('英俊的nick老師')
#
# # 多分支結(jié)構(gòu)
# '''
# if 條件1:
# code1 條件1成立執(zhí)行code1
# elif 條件2:
# code2 條件1不成立條件2成立執(zhí)行code2
# elif 條件3:
# code3 條件1和2不成立,條件3成立執(zhí)行code3
# elif可以有無限個。。。
# coden
# else:
# code4 所有條件都不成立,執(zhí)行code4
# '''
#
# '''
# height > 130 全票
# heigh > 70 and height < 130 半票
# height < 70 免票
# '''
#
# height = int(input('請輸入你的身高:'))
#
# if height > 130:
# print('全票')
# elif height > 70:
# print('半票')
# else:
# print('免票')
#
#
# if height>130:
# print('全票')
# if height <130 and height >70:
# print('半票')
# if height <70:
# print('免票')
#
# # 變量/if判斷/for循環(huán)
#
# '''
# if
#
# if
#
# if
# '''
# # 和
# '''
# if
#
# elif
#
# else
# '''
# # 的區(qū)別
#
#
# '''
# 如果 成績>=90,打印"優(yōu)秀"
# 如果 成績>=80 并且 成績<90,打印"良好"
# 如果 成績>=70 并且 成績<80,打印"普通"
# 其他情況:打印"差"
# '''
#
# grade = input('請輸入你的成績:')
#
# grade_int = int(grade)
#
# if grade_int>=90:
# print('優(yōu)秀')
# elif grade_int >=80:
# print('良好')
# elif grade_int >=70:
# print('普通')
# else:
# print('差')
#
#
# # 小伙子,給我做一個登錄功能。(模仿)
#
# # 難在條件的選擇,以及輸入/輸出的選擇# 找bug的絕招,打印變量,查看變量的變化過程 --》 debug的來源# x = int(input('x:'))
# print(1,x)
#
#
# if x > 100:
# x *= 10 # x = x*10
# print(2,x)
# elif x > 10:
# # code1
# # code2
# # code1
# # code2
# x /= 10 # x = x /10
# print(3,x)
# else:
# pass # 啥也不做,占個位置
#
# print(x)# 前期不要用debug模式運行,而是自己通過print查看變化
for i in range(10):print(i**2)# 5%語法錯誤
# 80%錯誤來自于邏輯錯誤(變量的變化方向沒有控制好)
# 10%粗心錯誤name ='nick'
name1= 'jason'# 流程控制:控制變量往一個方向變化# 循環(huán):重復(按照某種規(guī)律)干一件事# print(1)
# print(2)
# print(3)
# print(4)
# print(5)
# print(6)
# print(7)
# print(8)
# print(9)
# print(10)# print(1)
# print('nick')
# print(2)# while 當'''
while 條件: # 條件成立運行代碼,不成立結(jié)束while循環(huán)代碼 # 代碼執(zhí)行結(jié)束后會進入下一次循環(huán)(再一次判斷條件)
'''
# while 1:
# print(1)
#
# print(2)# while + break
# count = 0
# while 1:
# if count == 100:
# break # break終止循環(huán)
# count += 1
# print(count)
#
# print('bzr')# while + continue 不打印50
# count = 0
# while 1:
# if count == 100:
# break # break終止循環(huán)
# count += 1
# if count == 50:
# continue # continue跳出本次循環(huán),不執(zhí)行下面的代碼
# print(count)
#
# print('bzr')# 打印1-100內(nèi)偶數(shù)(不包括[22,46,68,98])的和
# 分解題目
# print(2525 - 22 - 46 - 68 - 98 + 25)
#
# count = 0
# sum_count = 0
# while True:
#
# if count == 100:
# break
#
# count += 2
# if count in [22, 46, 68, 98]:
# continue
#
# sum_count += count
#
# print(sum_count)# tag(中間變量)控制while循環(huán)
'''
count = 0
while count:pas
'''# count = 0# count = 98 # count = 100
# sum_count = 0
# while count < 100: #
#
# count += 2 # count = 100
# if count in [22, 46, 68, 98]:
# continue
#
# sum_count += count #
#
# print(sum_count)# while + else 僅作了解(非用不可可以使用,不要和if。。else混了)
# count = 0
# while count < 100:
# count += 1
# print(count)
# else:
# print('沒有被break干掉我就能出來')# count = 0
# while count < 50:
# if count == 100:
# break
# count += 1
# print(count)
# else: # 沒有被break干掉就執(zhí)行,被break終止了就不執(zhí)行
# print('沒有被break干掉我就能出來') # 可以判斷while是否被break終止# 猜年齡游戲,有三次復活機會age = 18
count = 0
while count < 3:age_inp = input('請輸入你的年齡:')age_inp_int = int(age_inp)if age_inp_int > age:print('猜大了')elif age_inp_int < age:print('猜小了')else:print('猜對了')breakcount += 1
轉(zhuǎn)載于:https://www.cnblogs.com/jinhongquan/p/11507030.html
總結(jié)
以上是生活随笔為你收集整理的基本运算符与if while详解:的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。