python學習第一天

一.python的數據類型
整數:程序表示上和數學上的寫法同樣,例如,1,100,-800等。
浮點數:小數。
字符串:以''或者" "擴起來的任意文本,‘自己也可做爲一個字符
轉義字符:\n表示換行 \t表示製表符 \自己也要轉義 \\表示 \ 須要加不少\的時候爲了 簡化,使用r''表示''
布爾值:布爾值和布爾代數的表示徹底一致,一個布爾值只有true 、False兩種值,在python中直接能夠使用Ture和False
空值:空值是python裏的一個特殊的值,用none表示,none不能理解爲0
常量:其值不能改變的變量,在python中,一般用所有大寫的變量名錶示常量,如PI=3.14
%d 整數
%f 浮點數
%s 字符串
%x 十六進制數
條件判斷
age = 20 if age >= 18: print('your age is', age) print('adult')
#
age = 3 if age >= 18: print('your age is', age) print('adult') else: print('your age is', age) print('teenager')
if <條件判斷1>: <執行1> elif <條件判斷2>: <執行2> elif <條件判斷3>: <執行3> else: <執行4>
輸入輸出
input
循環
names = ['Michael', 'Bob', 'Tracy'] for name in names: print(name)
 
循環
dic和set
與list比較,dict有如下幾個特色:
1.查找和插入速度極快,不會隨着key的增長而變慢
2.須要佔用大量的內存,內存浪費多。
函數
在python中,定義一個函數要使用def語句,依次寫出函數名、括號、括號中的參數和冒號:在縮進塊中編寫函數體,函數的返回值用return語句。
def greet_user():
print ("hello world!")
greet_user()
向函數傳遞信息
實參和形慘
實參:給變量一個指定的值。
形參:給變量一個形參,函數完成其工做所須要的一項信息。
 
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':
print (car.upper())
else:
print(car.title())
age=12
if age <4:
print ("Your admission cost is $0.")
elif age <18:
print ("Your admission cost is $5.")
else:
print ("Your admission cost is $10.")
 
age=12
if age <4:
price=0
elif age<18:
price=5
elif age <65:
price=10
else:
price=5
print ("Your admission cost is $"+str(price)+".")
 
字典
alien_0={'color':'green','point':5}
print (alien_0['color'])
print (alien_0['point'])
 
alien_0={'color':'green','points':5}
new_points=alien_0['points']
print ("You just earned"+str(new_points)+"points!")
 
 
alien_0={}
alien_0['color']='green'
alien_0['points']=5
print (alien_0)
 
 
alien_0={'color':'green'}
print ("The alien is"+alien_0['color']+".")
alien_0['color']='yellow'
print ("The alien is now:"+alien_0['color']+".")
 
輸入輸出
name=raw_input("Please enter Your name:")
print ("Hello,"+name+"!")
 
height=raw_input("How tail are you,in inches?")
height=int(height)
if height>=36:
print ("\n You're tall enough to ride!")
else:
print ("\n You'll be able to ride when you're a little older.")
 
 
x=1
while x <=5:
print (x)
x+=1
 
函數
# -*- coding: UTF-8 -*-
def greet_user():
print ("Hello!")
greet_user()
 
向函數傳遞參數
實參和形參
 
python的模塊
python的模塊是一個文件,以.py結尾
def print_func( par ):
print "Hello : ", par
return
導入特定的函數
from module_name import function1,fuction2,fuction3
在python中,根據約定,首字母大寫的 名稱指的是類。
——init_()默認建立
class Dog():
def __init__(self,name,age):
self.name=name
self.age=age
def sit(self):
print (self.name.title()+"is now sitting.")
def roll_over(self):
print (self.name.title()+"rolled over!")
 
第80頁
相關文章
相關標籤/搜索