网站能需要怎么做才不会被攻击,环保网站建设多少钱,连云港网站建设网站,wordpress 主题 mnewsPython有很多功能强大的机器学习和大数据分析包#xff0c;适合对大数据和人工智能感兴趣的同学学习。要想了解一门语言#xff0c;首先需要了解它的语法。本文将介绍Python的一些基础语法#xff0c;包括数据类型、变量类型、条件控制、循环结构等内容。废话少说#xff0…Python有很多功能强大的机器学习和大数据分析包适合对大数据和人工智能感兴趣的同学学习。要想了解一门语言首先需要了解它的语法。本文将介绍Python的一些基础语法包括数据类型、变量类型、条件控制、循环结构等内容。废话少说Here we go 目录 快速开始Hello, World! 数据类型数字 (Number)字符串 (String)常用字符串操作 数据结构列表 (List)常用内置函数 元组 (Tuple)字典 (Dictionary)常用内置函数 集合 (Set) 条件判断循环控制for...in循环while循环break和continue 迭代器和生成器列表生成式生成器迭代器 快速开始
首先你需要在你的电脑上安装Python。安装过程非常简单访问Python官网下载适合你系统的版本并安装。安装好后你可以在命令行中键入命令python进入交互模式也可以使用你喜欢的文本编辑器编写Python脚本然后在命令行中执行。
Hello, World! 几乎所有的编程语言的第一个程序都是打印hello, world!。以下是Python中的实现代码
greeting input(Enter your greeting: ) # 从键盘中输入
print(greeting) # 打印输入内容在Python3中print后面一定要加括号。你也可以将脚本保存为hello.py然后在命令行中执行python3 hello.py。输出如下
hello, world!数据类型
Python的语法简单的一个方面是它不需要声明变量类型。直接用给变量赋值后变量就被创建了。以下是Python中常见的数据类型
数字 (Number)
Python支持整数(int)浮点型(float)布尔型(bool)以及复数型(complex)。可以用type()函数来判断变量类型
a, b, c, d 10, 5.5, False, 25j
print(type(a)) # 输出class int
print(type(b)) # 输出class float
print(type(c)) # 输出class bool
print(type(d)) # 输出class complex字符串 (String) 字符串用单引号 或者双引号 括起来的任意字符表示。例如
str1 hello, world
str2 hello, world常用字符串操作
切片字符串可以方便地截取其中一部分。索引从0开始负索引从-1开始。
str python
print(str[0]) # 输出p
print(str[2:4]) # 输出th连接和复制用连接两个字符串用*复制字符串。
str1 hello
str2 world
print(str1 str2) # 输出hello world
print(str1 * 3) # 输出hellohellohello多行输出用...格式输出多行字符串。
print(line1
line2
line3)格式化输出类似C语言的printf。
name Mike
age 16
print(My name is %s and Im %d years old % (name, age))数据结构
列表 (List)
列表是Python中最重要的类型之一用方括号[]表示元素用逗号隔开。列表的常用操作包括
list [10, python, 8.2]
list1 list[0:2]
list2 list[1:]
list3 list * 2
print(python in list) # 输出True常用内置函数
list [10, 20, 30, 40]
print(len(list)) # 列表长度
print(max(list)) # 最大值
print(min(list)) # 最小值
list.append(50) # 添加元素
list.pop() # 删除末尾元素
print(list)元组 (Tuple)
元组和列表类似但元组中的元素不能修改用小括号()表示。
tuple1 (8, tuple, 6.6)
print(tuple1[1]) # 输出tuple字典 (Dictionary)
字典是无序的键值对集合用大括号{}表示。
dic {name: John, age: 25}
print(dic[name]) # 输出John常用内置函数
dic {name: John, age: 25}
print(dic.keys()) # 输出所有键
print(dic.values()) # 输出所有值
dic.pop(age) # 删除键age
print(dic)集合 (Set)
集合是无序且不重复的元素集合用大括号{}或者set()函数创建。
num {1, 2, 3, 4}
num.add(5) # 添加元素
num.remove(3) # 删除元素
print(num)条件判断
条件判断非常简单例如
num 10
if num 0:print(positive number!)
elif num 0:print(negative number!)
else:print(zero!)循环控制
Python主要有两种循环for...in循环和while循环。
for...in循环
可以遍历列表、元组、字符串等。
names [John, Mike, Bob]
for name in names:print(name)sum 0
for i in range(101):sum i
print(sum)while循环
n 10
sum 0
while n 0:n - 1sum n
print(sum)break和continue
for i in range(10):if i 5:breakprint(i)for i in range(10):if i 5:continueprint(i)迭代器和生成器 列表生成式
list1 [2 * a for a in range(10)]
list2 [a * b for a in range(3) for b in range(4)]
list3 [2 * a for a in range(10) if a % 2 1]
print(list1)
print(list2)
print(list3)生成器
generator1 (2 * a for a in range(10))
for n in generator1:print(n)def cube(num1, num2):for i in range(num1, num2):yield i ** 3g cube(3, 8)
for n in g:print(n)迭代器 from collections import Iterableprint(isinstance([], Iterable)) # 输出True
print(isinstance(iter([]), Iterable)) # 输出True以上是Python3最核心最基本的语法后面会继续更新Python高级知识祝大家学习愉快 文章转载自: http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.mlfmj.cn.gov.cn.mlfmj.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.ktskc.cn.gov.cn.ktskc.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.nqbpz.cn.gov.cn.nqbpz.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.dgng.cn.gov.cn.dgng.cn