python快速入门答案-python快速入门基础知识
1.变量赋值与语句
#python 不需要手动指定变量类型。不需要分号
#To assign the value 365 to the variable days,we enter the variable name, add an equals sign(=)
days=365
2.输出 print()
1 #print(),python3中,必须加括号。
2 number_of_days = 365
3 print('Hello python')
4 print(number_of_days)
3.常见数据类型(str,int,float)
str_test='China'int_test=365float_test=34.22
print(type(str_test)) #type()为输出数据类型
输出为:
4.LIST基础
数值类型转换
str_eight=str(8) # str()令8转化为字符型,字符型无法做计算操作
print(type(str_eight))
int_eight=int(str_eight)
print(type(int_eight))
输出:
常见计算符号:加法 + ;减法 -;乘法 *;n次幂 **n;
1 x=2
2 y=x**3
3 print(y)
4 #输出 8
1 #LIST 如何定义LIST类型,如第一行所示
2 months=[]
3 print(type(months))
4 print(months)
5 months.append("January")
6 months.append("February")
7 print(months)
8
9 #输出
10
11 []
12 ['January', 'February']
1 #LIST中元素的类型可以不同
2 months=[]
3 months.append(1)
4 months.append("January")
5 months.append(2)
6 months.append("February")
7 print(months)
8 #输出
9 [1, 'January', 2, 'February']
1 #LIST中寻找特定值
2 countries =[]
3 temperatures=[]
4
5 countries.append("China")
6 countries.append("India")
7 countries.append("United States")
8
9 temperatures.append(32.2)
10 temperatures.append(43.2)
11 temperatures.append(23.2)
12
13 print(countries)
14 print(temperatures)
15 china=countries[0] #取第一个元素
16 china_temperature = temperatures[1] #取第二个元素
17 print(china)
18 print(china_temperature)
#输出
['China', 'India', 'United States']
[32.2, 43.2, 23.2]
China
43.2
len() 得到LIST的元素数量
int_months = [1,2,3,4,5]
lenght = len(int_months) #包含多少个元素数量
print(lenght)
#输出 5
int_months = [1,2,3,4,5]
lenght = len(int_months) #包含多少个元素数量
index = len(int_months)-1last_value =int_months[index]
two_four = int_months[2:4] #冒号左右为起始位置(取)和结束位置(不取),。取左不取右
three_six = int_months[3:] #从第index=3(第四个元素)开始取到最后的值
print(lenght)
print(last_value)
print(two_four)
print(three_six)
#输出
5
5[3, 4]
[4, 5]
1 #查找LIST中是否存在特定元素
2
3 animals=["cat","ice","o""dog"]
4 if "cat" inanimals:
5 print("cat_found")
5.循环结构:
5.1 for循环
1 #python中通过缩进表示结构。
2 cities=[["China","aa","adfa"],["adfaf","adf2","2oo"]]
3 for city incities:
4 for j incity:
5 print (j)
输出:
China
aa
adfa
adfaf
adf2
2oo
5.2 while循环
1 i=0
2 while i<3:
3 i+=1
4 print(i)
输出:
1
2
3
range()
1 #range(5)代表0到4
2 for i in range(5):
3 print(i)
输出:
0
1
2
3
4
6.判断结构 &布尔类型(bool)
1 cat=True
2 dog=False
3 print(type(cat))
输出:
1 t=True
2 f=False
3 ift:
4 print("Now you see me")
5 iff:
6 print("Supring")
7 输出:
8 Now you see me
9
10 #输出0代表False,其他数字都代表True
7,字典
1 #dictionaries 字典结构,字典中Key表示键,value表示值,键值对一一对应
2 #字典中
3 scores={} #定义字典
4 print(type(scores))
5 scores["Jim"] = 80 #字典初始化方法1 变量名["键"]= Value
6 scores["Sue"] = 75
7 scores["Ann"] = 85
8 print(scores)
9 print(scores["Jim"])
10
11 #输出
12
13 {'Jim': 80, 'Sue': 75, 'Ann': 85}
14 80
另一种字典初始化方法
1 students={}
2 #字典初始化方法2
3 students={"Jim":80,"Sue":75,"Ann":85}
4 print(students)
5
6 #对字典中的Value进行操作
7 students["Jim"]=students["Jim"] + 5
8 print(students)
10 #输出
11 {'Jim': 80, 'Sue': 75, 'Ann': 85}
12 {'Jim': 85, 'Sue': 75, 'Ann': 85}
判断某键是否在字典中
#判断某键是否在字典中
#print("Jim" in students) #输出 True
if "Jim" instudents:
print("True")
else:
print("False")
#输出
True
用字典统计某LIST中特定元素的数量
1 pantry=["apple","orange","grape","apple","orange","orange","grape"]
2 pantry_counts={}
3
4 for item inpantry:
5 if item inpantry_counts:
6 pantry_counts[item] = pantry_counts[item] + 1
7 else:
8 pantry_counts[item]=1
9 print(pantry_counts)
10
11 #输出
12 {'apple': 2, 'orange': 3, 'grape': 2}
8.文件操作
1 f=open("C:\Users\*\Desktop\test.txt",'w')
2 f.write('12345')
3 f.write(' ')
4 f.write('23456')
5 f.close()
6 #清空文件并读入此2行数字,记得关闭文件。
1 weather_data=[]
2 f = open("C:\Users\Allen\Desktop\test\weather.csv",'r',encoding = 'utf-8-sig') #读取格式问题要注意。
3 data =f.read()
4 #rows = data.split(' ')
rows=list(filter(None,data.split(' ')))
5 for row inrows:
6 split_row = row.split(',',1000)
7 weather_data.append(split_row)
8 print( weather_data)
9
10 #输出,存在问题:读取内容多了最后一个空元素 #
11 [['1', 'Sunday'], ['2', 'Sunday'], ['3', 'Sunday'], ['4', 'Sunday'], ['5', 'Windy'], ['6', 'Windy'], ['7', 'Windy'], ['']]
改正后空字符消失
1 weather=[]
2 for row inweather_data:
3 weather.append(row[0]) #把row第一列的数据提取出来
4 print(weather)
5 f.close()
6
7 #输出
8 ['1', '2', '3', '4', '5', '6', '7', '']
8 函数
#def代表函数开始, def 函数名(传入参数) 传入参数无需定义类型
defprintHello():
print('Hello pythy')
return
defprintNum():
for i in range(4):
print(i)
return
defadd(a,b):
return a+b
printHello()
print(printNum())
print(add(3,4))
#输出
Hello pythy
0
1
2
3None
7
python快速入门答案-python快速入门基础知识相关推荐
- python编程中常用的12种基础知识总结
python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进 ...
- python六十七课——网络编程(基础知识了解)
网络编程: 什么是网络编程? 网络:它是一种隐形的媒介:可以将多台计算机使用(将它们连接到一起) 网络编程:将多台计算机之间可以相互通信了(做数据交互) 一旦涉及到网络编程,划分为两个方向存在,一方我 ...
- WPF入门0:WPF的基础知识
WPF入门0:WPF的基础知识 WPF 可创建动态的数据驱动的呈现系统. 系统的每一部分均可通过驱动行为的属性集来创建对象. 数据绑定是系统的基础部分,在每一层中均进行了集成. 传统的应用程序创建一个 ...
- python编程基础知识点总结_【转载】Python编程中常用的12种基础知识总结
Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进 ...
- Python 编程中常用的12种基础知识总结
Python 编程中常用的12 种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出 ...
- python快速入门答案-Python 开发 14 天快速入门
专栏亮点 零基础学习,循序渐进:专栏将编程语言的学习路线提炼为基础.中级.高级三层,内容由易到难,循序渐进,简练而生动地为读者呈现知识点. 内容全面,提炼要义:从核心概念到高级知识点,包括基本数据结构 ...
- python快速入门答案-Python 快速入门笔记(1):简介
本系列随笔是本人的学习笔记,初学阶段难免会有理解不当之处,错误之处恳请指正.转载请注明出处:https://www.cnblogs.com/itwhite/p/12290423.html. 语言简介 ...
- python编程基础知识点上的问题_python编程入门之二:必备基础知识
大家在上一章中已经可以自己敲出一个猜数字小游戏了,先不要删掉它,以后我们慢慢来改进它.接下来呢,我们再继续学习一些必须要学的基础知识. 2.1 变量 说到变量,就是可以改变的量,它并不是一个值,而是内 ...
- Python编程从入门到放弃 - Part 1基础知识习题解析
目录 第1章 起步 第2章 变量和简单数据类型 第3章 列表简介 第4章 操作列表 第5章 if语句 第6章 字典 第7章 用户输入和while循环 第8章 函数 第9章 类 第10章 文件和异常 第 ...
- Python编程从入门到实践 第一部分基础知识 代码合集
第2章 变量和简单数据类型 2.1 运行hello_world.py时发生的情况 print("Hello Python world!") 2.2 变量 message=" ...
最新文章
- 【翻译】SQL Server索引进阶:第三级,聚集索引
- MySQL创建数据库时指定编码和用户授权
- netstat 命令的 学习笔记
- wine和steam的区别
- 分布式事务Seata中的三个角色
- .NET Core 3.0稳定版发布
- java集合的添加方法_深入理解java集合框架之---------Arraylist集合 -----添加方法
- Maximum sum(poj 2479)
- spring boot+thmyleaf ModelAndView页面传值
- USB peripherals can turn against their users
- 文档丨Oracle数据库异构上云最佳实践
- java 计算 四分位,Java四分位计算方法
- eclipse 集成svn客户端_TortoiseSVN及Eclipse的svn插件安装使用
- [Unity]限制一个值的大小(Clamp以及Mathf)
- FISCO BCOS Solidity 使用Table合约CRUD接口 智能合约例子
- 微服务:知识点梳理(SOA、服务拆分、服务治理、分布式事务)
- matlab中双x轴,【转】MATLAB:双X轴曲线绘图
- 日历控件CalendarView
- 8.微信小程序-Mobx数据共享(类似vuex)
- 计算机一打开就卡在更新失败,做系统一直在正在启动画面-电脑开机后卡在“正在启动windows”界面,怎么办?...
热门文章
- 机器学习经典分类算法 —— C4.5算法(附python实现代码)
- nodejs入门教程之http的get和request简介及应用
- 操作dict时避免出现KeyError的几种方法
- Mybatis怎么在mapper中用多个参数
- java_method_下拉框成json
- 前端资源构建-Grunt环境搭建
- poj1815最小割
- asp.net入门详细介绍
- python自学教程读书导图-自学Python第一天:起点读书自动领取经验值(附思路讲解)...
- python中类方法与实例方法的区别-Python实例方法、静态方法和类方法详解(包含区别和用法)...