Python 3.5.4 (v3.5.4:3f56838976, Aug 7 2017, 12:56:33)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world')
hello world
>>> exit()1. 交互式運(yùn)行環(huán)境備注---說明

2.交互式環(huán)境使用場景---問題
3.腳本方式運(yùn)行代碼
練習(xí):
print('Hello World'+'+'+ str(2))
print(2*3.1415926*10)
print(3.1415926*10*10)
print('100+2=',100 +2)
print('1-5 = ',1-5)
print('1*5 = ',1*5)
print('1/5 = ',1/5)
-》結(jié)果輸出
Hello World+2
62.831852
314.15926
100+2= 102
1-5 = -4
1*5 = 5
1/5 = 0.2變量定義&賦值
a_pi = 3.1415926
ra = 10
print (2*a_pi*ra)
print(a_pi*ra*ra)
輸出結(jié)果:
62.831852
314.15926定義變量
pi=3.1415926
area = pi * radius ** 2注:變量命名規(guī)則:
整數(shù)、浮點(diǎn)數(shù)、正數(shù)、負(fù)數(shù)
像年齡、身高、體重、分?jǐn)?shù)、圓周率這樣的數(shù)字
height = 1.71
age =29
wiht =140
pi = 3.1415926
score = 5.5數(shù)據(jù)類型的運(yùn)算---四則運(yùn)算
除(/)
print(type(1))
print(type(1.0))
print(type(''))type結(jié)果:
<class 'int'>
<class 'float'>
<class 'str'>
* 類型相互轉(zhuǎn)化
- int/str => float
- float/str => int
- int/float => str
```python
print(type(int(1.9)))
print(type(int(2)))
print(type(float(1)))
print(type(str(1)))
print(type(str(1.8)))
類型輸出結(jié)果
<class 'int'>
<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>字符串類型
使用單引號、雙引號、三個(gè)單引號或三個(gè)雙引號引起來的一些字符
name = 'dxy'
job = "linux"
特殊字符
\ 轉(zhuǎn)義符
\r 回車
\n 換行
\t tab鍵
\f 換頁
print("i 'm dxy")
print('i\'m dxy')
print('a \nb \tc ')
print('a\\nb\\tc\\')
練習(xí)
name = str('dxy')
age = int('20')
input('please name and age->:')
print('My name is',name,'Im,',age,'years old')
提示用戶從控制臺輸入一個(gè)分?jǐn)?shù)
#妻子的想法
momeny = 100
prompt = input('看到賣西瓜的了嗎?(Y/N)')
if prompt =='Y':
print('買一斤包子需要花費(fèi):10元')
momeny -= 10
if prompt =='Y':
print('買一個(gè)西瓜需要花費(fèi):20元')
momeny -= 20
print('剩余金額'+ str(momeny))
#老公想法
momeny =100
prompt1 = input('看到賣西瓜的了嗎?(Y/N)')
if prompt1 == 'Y':
print('買一個(gè)包子需要:3元')
momeny -= 3
else:
print('買一斤包子需要:10元')
momeny -= 10
print('剩余金額'+str(momeny))根據(jù)表達(dá)式的真假控制代碼的是否結(jié)束子語句循環(huán)執(zhí)行,如果為真則繼續(xù)循環(huán)執(zhí)行
total = 0
idx = 1
while idx <= 100:
total = total+ idx
idx = idx+1print(total)
練習(xí)
1. 循環(huán)提示用戶在控制臺上輸入數(shù)字或者exit,當(dāng)用戶輸入exit后結(jié)束程序,并打印所有輸入數(shù)字的和與平均數(shù)
```python
total = 0
count = 0
input_number = ''
while input_number !='exit':
input_number = input('請輸入一個(gè)數(shù)字--:')
if input_number != 'exit':
total += float(input_number)
count += 1
if count !=0:
print('total',total,'avg',total/count)
else:
print('total:', total, ', avg:', 0)break 跳出循環(huán)
continue 跳過本次循環(huán),繼續(xù)下一次循環(huán)條件判斷
idx = 0
while idx <= 10:
idx += 1
if idx == 4:
continue
else:
if idx ==9:
break
print(idx)
nums = [1, 5, 6, 3, 2, 5]
for nums1 in nums:
print(nums1)作業(yè)
打印乘法口訣
提示:嘗試print(‘kk’)與print(‘kk’, end=‘’)的區(qū)別
x = 0
while x <9:
x += 1
# print(x)
y=0
# print(y)
while y < x:
y += 1
print("%d*%d=%2d" % (x,y,x*y),end=" ")
print('\n')import random
num_random = random.randint(0,100)
count = 1
while True:
input_num = int(input('游戲限制輸入5次結(jié)束,請慎重輸入>>'))
if input_num ==num_random:
print('高手!猜對了')
break
elif input_num > num_random:
print('猜大了!!小伙伴')
else:
print('猜小了!!小伙伴')
count =count+1
if count > 2:
print('太笨了,下次再來,正確的數(shù)字是',int(num_random))
break
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文標(biāo)題:python入門(一)-創(chuàng)新互聯(lián)
URL標(biāo)題:http://chinadenli.net/article30/eccpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站收錄、商城網(wǎng)站、小程序開發(fā)、微信公眾號、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容