程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes

編輯:Python

day1

# File operations
fp = open('D:/text.txt','a+')
print('hello word',file = fp)
fp.close()
# Variables have three properties
name = ' Xiao Benjie '
print(' Memory ',id(name))
print(' type ',type(name))
print(' value ',name)
# Variable string connection
name = ' Li Si '
age = 378
print(' My name is ' +name+ ' This year, ' +str(age)+ ' year ')
# prompt ( The default variable is string type )
input() The return value is str type
a = int(input(' Please enter the first addend '))
b = int(input(' Please enter the second addend '))
print(a+b)
# Division operation
print(11//2) #// Integer division operator Output 5
# Secondary operation
print(2**3) # Express 2 Third power
# Chained assignment
a=b=c=20
# Unpack assignment
a,b,c=20,30,40
# Exchange two variable values
a,b=20,30
a,b = b,a
print(a,b) // Output 30,20
# Comparison operator ( identification , type , value )
>,<,>=,<=,!=,== It's right vaue Compare
is, is not It's right id Compare
The return result is Boolean
a,b=20,30
print('a>b Do you ',a>b) // Output false
# Logical operators
a,b=1,2
s='hello word'
print(a==1 and b==2)
print(a==1 or b==3)
print(not a) //false
print('w' in s) //true
print('w' not in s) //false
# An operation
& | << >>

# Branching structure 
money=int(input(' Enter an integer '))
if money<100:
print(' The amount is less than 100')
if money%2!=0:
print(' Amount is odd ')
else:
print(' The amount is even ')
elif money>100 and money<200:
print(' Amount is greater than 100 Less than 200')
else:
print(' Amount is greater than 200')

day2

# Conditional expression
'''
grammar :
x if Judge the condition else y
Operational rules : The judgment condition is true The return value is x, The judgment condition is false The return value is y
'''
num_a = int(input(' Input a Value '))
num_b = int(input(' Input b Value '))
print(str(num_a)+' Greater than '+str(num_b) if num_a>num_b else str(num_a)+' Less than or equal to '+str(num_b))
#pass sentence
Don't do anything? , Just a placeholder , Use where you need to write a statement
answer = input(' Are you a member ?')
if answer == 'y':
pass
else:
pass
#range()
r=range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], The default from the 0 Start , Default phase difference 1 Become a step
print(r) # Output range(0,10) The return value is an iterator
print(list(r)) # For viewing range Object Output [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
r=range(1,10) #[1, 2, 3, 4, 5, 6, 7, 8, 9], from 1 Start ,10 end , The default step size is 1
r=range(1,10,2) #[1, 3, 5, 7, 9], from 1 Start to 10 end , In steps of 2
print(10 in r) #false Judge 10 Whether in r In sequence
print(10 not in r) #true
# loop
#while loop
while Conditions :
The loop body
#for loop
for item in 'Python':
print(item) // Output P y t h o n
for item in range(10):
print(item) // Output 0,1,2,3,4,5,6,7,8,9
for _ in range(5): // Cycle over and over 5 All over 
print(' Life is too short , I use Python')
#else Collocation cycle
When the loop executes normally ( I haven't met break), Will execute else sentence
for:
else:
while:
else:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved