python基础语法

1. 基本使用

1.1 print 功能

#print字符串
print('hello world')
print("hello world")
hello world
hello world
#print(字符串叠加)
print('hello world!'+' hello xian!')
hello world! hello xian!
#简单运算
print(1+1)
print(3-1)
print(4*6)
print(10/2)
2
2
24
5.0
print('hello'+5) #字符串不可以直接和数字相加
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-7-8a7037cca0ae> in <module>
----> 1 print('hello'+5) #字符串不可以直接和数字相加


TypeError: can only concatenate str (not "int") to str
print(int('2')+3)  #int为定义整型
print(int(1.9))   #强制类型转换
print(float('1.2')+3)  #float()是浮点型,可以把字符串转换成小数
5
1
4.2

1.2 基础数学运算

#基本数学运算
1+8  #加法
9
9-2  #减法
7
3*8  #乘法
24
4/3  #除法
1.3333333333333333
3**3  #**与^等价
27
8%3  #取余
2

1.3变量 Variable

#自变量命名规则
name  = 10000  #赋值数字
print(name)
name = 'my school'  #赋值字符串
print(name)
my_name = 'dog'  #变量命名下划线
print(my_name)
a,b,c = 15, 16, 17  #一次定义多个自变量
print(a,b,c)
10000
my school
dog
15 16 17

2. while和for循环

2.1while循环

#基本使用while循环
#   while condition:
#        expressions
condition = 0
while condition < 10:
    print(condition)
    condition += 1
0
1
2
3
4
5
6
7
8
9
#数字:整数和浮点数也能进行 Boolean 数据操作, 具体规则,如果该值等于 0 或者 0.0 将会返回 False 其余的返回 True
#None 类型:如果 while 后面接着的语句数据类型 None, 将会返回 False。
#集合类型:在 Python 中集合类型有 list、 tuple 、dict 和 set 等,如果该集合对象作为 while 判断语句, 如果集合中的元素数量为 0,那么将会返回 False, 否则返回 True。
a = range(10)
while a:
    print(a[-1])
    a = a[:len(a)-1]
#上述程序将会返回 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 程序首先判断列表是否空,如果不为空,则 打印出最后一个内容,然后使用切片操作去掉最后一个元素,并更新列表;如此重复,直至列表为空。
9
8
7
6
5
4
3
2
1
0

2.2 for 循环

#for循环
# for item in sequence:
#       expressions
#sequence 为可迭代的对象,item 为序列中的每个对象。
example_list = [1,2,3,4,5,6,7,12,543,876,12,3,2,5]
for i in example_list:
    print(i)
1
2
3
4
5
6
7
12
543
876
12
3
2
5
#range使用  在 Python 内置了工厂函数,range 函数将会返回一个序列
#1 range(start, stop) start 将会是序列的起始值,stop为结束值,但是不包括该值,类似 数学中的表达 [start, stop),左边为闭区间,右边为开区间。

for i in range(1, 10):
    print(i)
1
2
3
4
5
6
7
8
9
#2 range(stop)  如果省略了 start 那么将从 0 开始,相当于 range(0, stop)
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9
#3 range(start, stop, step) step 代表的为步长,即相隔的两个值得差值。从 start 开始,依次增加 step 的值,直至等于或者大于 stop
for i in range(0,13, 5):
    print(i)
0
5
10
#内置集合Python 共内置了 list、 tuple 、dict 和 set 四种基本集合,每个 集合对象都能够迭代
#tuple 类型
tup = ('python', 2.7, 64)
for i in tup:
    print(i)
python
2.7
64
#dictionary 类型
dic = {
   }
dic['lan'] = 'python'
dic['version'] = 2.7
dic['platform'] = 64
for key in dic:
    print(key, dic[key])
# 字典在迭代的过程 中将 key 作为可迭代的对象返回。注意字典中 key 是乱序的,也就是说和插入 的顺序是不一致的。如果想要使用顺序一致的字典,请使用 collections 模块 中的 OrderedDict 对象
lan python
version 2.7
platform 64
#set 类型
s = set(['python', 'python2', 'python3','python'])
for item in s:
    print(item)
#将会输出 python, python3, python2 set 集合将会去除重复项,注意输出的 结果也不是按照输入的顺序。
python
python3
python2
#迭代器
#Python 中的 for 句法实际上实现了设计模式中的迭代器模式 ,所以我们自己也可以按照迭代器的要求自己生成迭代器对象,
#以便在 for 语句中使用。 只要类中实现了 __iter__ 和 next 函数,那么对象就可以在 for 语句中使用。 现在创建 Fibonacci 迭代器对象,
# define a Fib class
class Fib(object):
    def __init__(self, max):
        self.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值