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=365
float_test=34.22
print(type(str_test)) #type()为输出数据类型
输出为:<class 'str'>

4.LIST基础

数值类型转换

str_eight=str(8) # str()令8转化为字符型,字符型无法做计算操作
print(type(str_eight)) int_eight=int(str_eight)
print(type(int_eight))
输出:
<class 'str'>
<class 'int'>

常见计算符号:加法 + ;减法 -;乘法 *;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 <class 'list'>
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)-1
last_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" in animals:
5 print("cat_found")

5.循环结构:

5.1 for循环

1 #python中通过缩进表示结构。
2 cities=[["China","aa","adfa"],["adfaf","adf2","2oo"]]
3 for city in cities:
4 for j in city:
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))
输出:
<class 'bool'>

 1 t=True
2 f=False
3 if t:
4 print("Now you see me")
5 if f:
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 <class 'dict'>
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" in students:
print("True")
else:
print("False") #输出
True

用字典统计某LIST中特定元素的数量

 1 pantry=["apple","orange","grape","apple","orange","orange","grape"]
2 pantry_counts={}
3
4 for item in pantry:
5 if item in pantry_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('\n')
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('\n')
rows=list(filter(None,data.split('\n')))
5 for row in rows:
6 split_row = row.split(',',1000)
7 weather_data.append(split_row)
8 print( weather_data)
9
10 #输出,存在问题:读取内容多了最后一个空元素 # Python3 三种办法解决split结果包含空字符串的问题 https://blog.csdn.net/qq523176585/article/details/83003346
11 [['1', 'Sunday'], ['2', 'Sunday'], ['3', 'Sunday'], ['4', 'Sunday'], ['5', 'Windy'], ['6', 'Windy'], ['7', 'Windy'], ['']]
改正后空字符消失
1 weather=[]
2 for row in weather_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 函数名(传入参数)   传入参数无需定义类型
def printHello():
print('Hello pythy') return def printNum():
for i in range(4):
print(i)
return def add(a,b):
return a+b printHello()
print(printNum())
print(add(3,4)) #输出
Hello pythy
0
1
2
3
None
7

python快速入门基础知识的更多相关文章

  1. 一、python快速入门(每个知识点后包含练习)

    1. 编程与编程语言 编程的目的是什么? #计算机的发明,是为了用机器取代/解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑/电脑一 ...

  2. 深度学习入门者的Python快速教程 - 基础篇

      5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5.1.1 Python简史 Python是一门解释型的高级编程语言,特点是简单明 ...

  3. Linux入门基础知识

    注:内容系兄弟连Linux教程(百度传课:史上最牛的Linux视频教程)的学习笔记. Linux入门基础知识 1. Unix和Linux发展历史 二者就像父子关系,当然Unix是老爹.1965年,MI ...

  4. Python快速入门PDF高清完整版免费下载|百度云盘

    百度云盘:Python快速入门PDF高清完整版免费下载 提取码:w5y8 内容简介 这是一本Python快速入门书,基于Python 3.6编写.本书分为4部分,第一部分讲解Python的基础知识,对 ...

  5. Python快速入门

    Python快速入门 一.基础概要 命名:h.py Linux命令行运行:python h.py 注释.数字.字符串: 基本类型只有数字与字符串 #python注释是这样写的 ''' 当然也可以这样 ...

  6. Python旅途——入门基础

    1.入门 ​ 作为近几年计算机程序设计语言中很火的Python,是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大 ...

  7. Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级

    前文5.2节和5.4节分别就Python下读文件操作做了基础性讲述和提升性介绍,但是仍有些问题,比如在5.4节里涉及到一个多次读文件的问题,实际上我们还没有完全阐述完毕,下面这个图片的问题在哪呢? 问 ...

  8. USB入门基础知识(转)

    源:USB入门基础知识 相关名词: 主机(Host) 设备(Device) 接口(Interface) 管道(Pipe) 管道是主机与设备端点数据传输的连接通道,代表了主机的数据缓冲区与设备端点之间交 ...

  9. React Native 入门基础知识总结

    中秋在家闲得无事,想着做点啥,后来想想,为啥不学学 react native.在学习 React Native 时, 需要对前端(HTML,CSS,JavaScript)知识有所了解.对于JS,可以看 ...

随机推荐

  1. 【51nod1462】树据结构

    Source and Judge 51nod1462 Analysis 请先思考后再展开 dffxtz师兄出的题 做法一:暴力树剖+分块,时间复杂度为 $O(nlognsqrt n)$ 做法二:利用矩 ...

  2. 《数据结构与算法》—— O(3N)=O(N) ?

    上帝的磨盘转动很慢,但是却磨得很细. --毛姆 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles ...

  3. C语言程序设计100例之(31):全排列问题

    例31   全排列问题 题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入格式 n(1≤n≤9) 输出格式 由1-n组成的所有不重复的数字 ...

  4. NS域名工作原理及解析

    DNS域名工作原理及解析   0x00 定义 DNS( Domain Name System)是“域名系统”的英文缩写,它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.D ...

  5. iOS中的分类和扩展

    一.什么是分类? 概念:分类(Category)是OC中的特有语法,它是表示一个指向分类的结构体指针.根据下面源码组成可以看到它没有属性列表,原则上是不能添加成员变量(其实可以借助运行时功能,进行关联 ...

  6. Nginx之反向代理配置(一)

    前文我们聊了下Nginx作为web服务器配置https.日志模块的常用配置.rewrite模块重写用户请求的url,回顾请参考https://www.cnblogs.com/qiuhom-1874/p ...

  7. 【Amaple教程】2. 模块

    正如它的名字,模块用于amaplejs单页应用的页面分割,所有的跳转更新和代码编写都是以模块为单位的. 定义一个模块 一个模块由<module>标签对包含,内部分为template模板.J ...

  8. iview中select搜索

    https://www.jianshu.com/p/1c40d7cc440e https://www.cnblogs.com/yun1108/p/10967735.html https://blog. ...

  9. Promise,Generator,Await/Async

    上节中忘记讲:Iterator接口和Generator函数的关系了,Symbol.iterator方法的最简单的实现就是通过Generator函数: let myIterable = { [Symbo ...

  10. Xcode调试之exc_bad_access以及 message sent to deallocated instance

    如果出现exc_bad_access错误,基本上是由于内存泄漏,错误释放,对一个已经释放的对象进行release操作.但是xcode有时候不会告诉你错误在什么地方(Visual Studio这点做得很 ...