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页

转载于:https://www.cnblogs.com/networking/p/11096873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值