码迷,mamicode.com
首页 > 编程语言 > 详细

python----------入门基础

时间:2016-05-11 11:15:35      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

 

简单的数学运算

整数相加,得到整数:

1+1
2
10+10
20

浮点数相加,得到浮点数:

7.2+5.8
13
5.5+2
7.5

整数和浮点数相加,得到浮点数:

3+3.2
6.2
2+2.1
4.1

变量赋值

Python使用<变量名>=<表达式>的方式对变量进行赋值 

左边变量名,右边表达式,不可调换位置。

a = 1
b = 2

字符串 String

字符串的生成,需要用单引号或双引号(引号必须成对出现,否则会出错):

s = "hello world"
s
‘hello world‘
s = ‘hello world‘
s
‘hello world‘

字符串的加法:

s = "hello" + " world"
s
‘hello world‘

字符串索引:

s = "hello world"
s[0]#python的第一位数是从0开始 不是1
‘h‘
s[-1]
‘d‘
s[0:5]
‘hello‘

字符串的分割:

s = "hello world"
s.split()
[‘hello‘,‘world‘]

查看字符串的长度:

s = "hello world"
len(s)
11

  

列表 List

Python用[]来生成列表:

a = [1,2,11,‘hello‘,1+1,5.5]
a
[1,2,11,‘hello‘,2,5.5]

列表加法:

a = [1, 2, 11, ‘hello‘, 2, 5.5]
a+a
[1, 2, 11, ‘hello‘, 2, 5.5, 1, 2, 11, ‘hello‘, 2, 5.5]

列表索引:

a = [1,2,11,‘hello‘,1+1,5.5]
a[1]
2

列表长度:

a = [1,2,11,‘hello‘,1+1,5.5]
len(a)
6

向列表中添加元素:

a = [1,2,11,‘hello‘,1+1,5.5]
a.append(‘"world")
a
[1, 2, 11, ‘hello‘, 2, 5.5, ‘world‘]

集合 Set

Python用{}来生成集合,集合中不能有相同元素:

s = {2, 3, 4, 2}
s
{2, 3, 4}

集合的长度:

s = {2, 3, 4}
len(s)
3

向集合中添加元素:

s = {2, 3, 4}
s.add(1)
s
{1, 2, 3, 4}

集合的 交:

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
a&b
{2,3,4}

集合的 并:

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
a | b
{1, 2, 3, 4, 5}

集合的 差:

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
a - b
{1}

集合的 对称差:

a = {1, 2, 3, 4}
 b = {2, 3, 4, 5}
a ^ b
{1, 5}

 

 未完.待添加


 

python----------入门基础

标签:

原文地址:http://www.cnblogs.com/zpython/p/5480764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!