1. 程式人生 > >1-python入門1

1-python入門1

div left pytho address username lock 1-1 numbers ear

內存管理: python 變量重新賦值之後,對應的內存地址會變

引用計數

del是解除綁定

內存地址復用

>>> x=1
>>> id(x)
9310240
>>> x=2
>>> id(x)
9310272
>>> y=1
>>> id(y)
9310240

字符串格式化控制符

print(‘用戶名是: %s 密碼是: %s‘ %)

使用while循環打印1 2 3 4 5 6 8 9 10

x = 1
print(‘Now, Print the numbers: ‘, end=‘ ‘)
while x < 11:
    print(x, end=‘ ‘)
    if x == 6:
        x += 2
        print("  ", end=‘‘)
    else:
        x += 1

方法二

print(‘\n‘)
x = 0
while x < 10:
    x += 1
    if x == 7:
        continue
    print(x, end=‘ ‘)

求1-100的和

print(‘\n‘)
y = 1
sum = 0
while y < 101:
    sum += y
    y += 1
print(‘1-100的所有數的和是:‘, sum)

print(‘\n‘)

求 1-100內所有的偶數

print(‘100以內所有的偶數: ‘, end=‘‘)
for i in range(1, 101):
    if i % 2 == 0:
        print(i, end=‘ ‘)

求100以內的奇數

print(‘\n‘)
print(‘100以內所有的奇數: ‘, end=‘ ‘)
for ii in range(1, 101):
    if ii % 2 != 0:
        print(ii, end=‘ ‘)

print(‘\n‘)

求1-2+3-4+5 ... 99的所有數的和

sum = 0
for i in range(1, 100):
    if i % 2 == 0:
        sum -= i
        print(‘-‘, i, end=‘‘)
    else:
        sum += i
        print(‘+‘, i, end=‘‘)
print(‘=‘, sum)

用戶登陸(三次機會重試)

username = (‘cx2c‘)
password = (‘cx2c‘)
for i in range(1,4):
    user=input(‘Please Input Your Name: ‘)
    pasw=input(‘Please Input Your Password: ‘)
    if username == user and password==pasw:
        print(‘Login in successful‘)
        break
    else:
        print(‘The %sth time False,Try Again‘ %i)

打印結果

Now, Print the numbers:  1 2 3 4 5 6   8 9 10 

1 2 3 4 5 6 8 9 10 

1-100的所有數的和是: 5050


100以內所有的偶數: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

100以內所有的奇數:  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 

+ 1- 2+ 3- 4+ 5- 6+ 7- 8+ 9- 10+ 11- 12+ 13- 14+ 15- 16+ 17- 18+ 19- 20+ 21- 22+ 23- 24+ 25- 26+ 27- 28+ 29- 30+ 31- 32+ 33- 34+ 35- 36+ 37- 38+ 39- 40+ 41- 42+ 43- 44+ 45- 46+ 47- 48+ 49- 50+ 51- 52+ 53- 54+ 55- 56+ 57- 58+ 59- 60+ 61- 62+ 63- 64+ 65- 66+ 67- 68+ 69- 70+ 71- 72+ 73- 74+ 75- 76+ 77- 78+ 79- 80+ 81- 82+ 83- 84+ 85- 86+ 87- 88+ 89- 90+ 91- 92+ 93- 94+ 95- 96+ 97- 98+ 99= 50
Please Input Your Name: cx2c
Please Input Your Password: cc
False
Please Input Your Name: cx2c
Please Input Your Password: cx2c
Login in successful

1-python入門1