从Zero到Hero,一文掌握Python关键代码
# 01基础篇
# 变量
#int
one=1
some_number=100 print("one=",one) #print type1
print("some_number=",some_number) #python3 print need add () """output
one= 1
some_number= 100
"""
#booleans
true_boolean=True
false_boolean=False print("true_boolean:",true_boolean)
print("False_boolean;",false_boolean) """output
true_boolean: True
False_boolean; False
"""
#string
my_name="leizeling" print("my_name:{}".format(my_name)) #print type2 """output
true_boolean: True
False_boolean; False
"""
#float
book_price=15.80 print("book_price:{}".format(book_price)) """output
book_price:15.8
"""
# 控制流:条件语句
#if
if True:
print("Hollo Python if")
if 2>1:
print("2 is greater than 1") """output
Hollo Python if
2 is greater than 1
"""
#if……else
if 1>2:
print("1 is greater than 2")
else:
print("1 is not greater than 2") """output
true_boolean: True
False_boolean; False
"""
#if……elif……else
if 1>2:
print("1 is greater than 2")
elif 1<2:
print("1 is not greater than 2")
else:
print("1 is equal to 2") """output
1 is not greater than 2
"""
# 循环/迭代器
#while
num=1
while num<=10:
print(num)
num+=1 """output
1
2
3
4
5
6
7
8
9
10
"""
#for
for i in range(1,11):#range(start,end,step),not including end
print(i) """output
1
2
3
4
5
6
7
8
9
10
"""
# 02列表:数组数据结构
#create integers list
my_integers=[1,2,3,4,5]
print(my_integers[0])
print(my_integers[1])
print(my_integers[4]) """output
1
2
5
"""
#create str list
relatives_name=["Toshiaki","Juliana","Yuji","Bruno","Kaio"]
print(relatives_name) """output
['Toshiaki', 'Juliana', 'Yuji', 'Bruno', 'Kaio']
"""
#add item to list
bookshelf=[]
bookshelf.append("The Effective Engineer")
bookshelf.append("The 4 Hour Work Week")
print(bookshelf[0])
print(bookshelf[1]) """output
The Effective Engineer
The 4 Hour Work Week
"""
# 03字典:键-值数据结构
#dictionary struct
dictionary_example={"key1":"value1","key2":"value2","key3":"value3"}
dictionary_example """output
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
"""
#create dictionary
dictionary_tk={"name":"Leandro","nickname":"Tk","nationality":"Brazilian"}
print("My name is %s"%(dictionary_tk["name"]))
print("But you can call me %s"%(dictionary_tk["nickname"]))
print("And by the waay I am {}".format(dictionary_tk["nationality"])) """output
My name is Leandro
But you can call me Tk
And by the waay I am Brazilian
"""
#dictionary add item
dictionary_tk["age"]=24
print(dictionary_tk) """output
{'nationality': 'Brazilian', 'nickname': 'Tk', 'name': 'Leandro', 'age': 24}
"""
#dictionayr del item
dictionary_tk.pop("age")
print(dictionary_tk) """output
{'nationality': 'Brazilian', 'nickname': 'Tk', 'name': 'Leandro'}
"""
# 04迭代:数据结构中的循环
#iteration of list
bookshelf=["The Effective Engineer","The 4 hours work week","Zero to One","Lean Startup","Hooked"]
for book in bookshelf:
print(book) """output
The Effective Engineer
The 4 hours work week
Zero to One
Lean Startup
Hooked
"""
#哈希数据结构(iteration of dictionary)
dictionary_tk={"name":"Leandro","nickname":"Tk","nationality":"Brazilian"}
for key in dictionary_tk: #the attribute can use any name,not only use "key"
print("%s:%s"%(key,dictionary_tk[key]))
print("===================")
for key,val in dictionary_tk.items(): #attention:this is use dictionary_tk.items().not direct use dictionary_tk
print("{}:{}".format(key,val)) """output
nationality:Brazilian
nickname:Tk
name:Leandro
===================
nationality:Brazilian
nickname:Tk
name:Leandro
"""
# 05 类和对象
#define class
class Vechicle:
pass
#create instance
car=Vechicle()
print(car) ""output
<__main__.Vechicle object at 0x000000E3014EED30>
"""
#define class
class Vechicle(object):#object is a base class
def __init__(self,number_of_wheels,type_of_tank,seating_capacity,maximum_velocity):
self.number_of_wheels=number_of_wheels
self.type_of_tank=type_of_tank
self.seating_capacity=seating_capacity
self.maximum_velocity=maximum_velocity
def get_number_of_wheels(self):
return self.number_of_wheels
def set_number_of_wheels(self,number):
self.number_of_wheels=number #create instance(object)
tesla_models_s=Vechicle(4,"electric",5,250)
tesla_models_s.set_number_of_wheels(8)#方法的调用显得复杂
tesla_models_s.get_number_of_wheels() """output
8
"""
#use @property让方法像属性一样使用
#define class
class Vechicle_1(object):#object is a base class
def __init__(self,number_of_wheels,type_of_tank,seating_capacity,maximum_velocity):#first attr must is self,do not input the attr
self._number_of_wheels=number_of_wheels ###属性名和方法名不要重复
self.type_of_tank=type_of_tank
self.seating_capacity=seating_capacity
self.maximum_velocity=maximum_velocity
@property #读属性
def number_of_wheels(self):
return self._number_of_wheels
@number_of_wheels.setter #写属性
def number_of_wheels(self,number):
self._number_of_wheels=number
def make_noise(self):
print("VRUUUUUUUM") tesla_models_s_1=Vechicle_1(4,"electric",5,250)
print(tesla_models_s_1.number_of_wheels)
tesla_models_s_1.number_of_wheels=2
print(tesla_models_s_1.number_of_wheels)
print(tesla_models_s_1.make_noise()) """output
4
2
VRUUUUUUUM
None
"""
# 06封装:隐藏信息
#公开实例变量
class Person:
def __init__(self,first_name):
self.first_name=first_name
tk=Person("TK")
print(tk.first_name)
tk.first_name="Kaio"
print(tk.first_name) """output
TK
Kaio
"""
#私有实例变量
class Person:
def __init__(self,first_name,email):
self.first_name=first_name
self._email=email
def get_email(self):
return self._email
def update_email(self,email):
self._email=email
tk=Person("TK","tk@mail.com")
tk.update_email("new_tk@mail.com")
print(tk.get_email()) """output
new_tk@mail.com
"""
#公有方法
class Person:
def __init__(self,first_name,age):
self.first_name=first_name
self._age=age
def show_age(self):
return self._age
tk=Person("TK",24)
print(tk.show_age()) """output
24
"""
#私有方法
class Person:
def __init__(self,first_name,age):
self.first_name=first_name
self._age=age
def _show_age(self):
return self._age
def show_age(self):
return self._show_age() #此处的self不要少了
tk=Person("TK",24)
print(tk.show_age()) """output
24
"""
#继承
class Car:#父类
def __init__(self,number_of_wheels,seating_capacity,maximum_velocity):
self.number_of_wheels=number_of_wheels
self.seating_capacity=seating_capacity
self.maximum_velocity=maximum_velocity
class ElectricCar(Car):#派生类
def __init(self,number_of_wheels_1,seating_capacity_1,maximum_velocity_1):
Car.__init__(self,number_of_wheels_1,seating_capacity_1,maximum_velocity_1) #利用父类进行初始化 my_electric_car=ElectricCar(4,5,250)
print(my_electric_car.number_of_wheels)
print(my_electric_car.seating_capacity)
print(my_electric_car.maximum_velocity) """output
4
5
250
"""
注:从机器之心转载
从Zero到Hero,一文掌握Python关键代码的更多相关文章
- 从零上手Python关键代码
来源 https://www.kesci.com/home/project/59e4331c4663f7655c499bc3
- Python实现代码统计工具——终极加速篇
Python实现代码统计工具--终极加速篇 声明 本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对 ...
- LDA处理文档主题分布代码
[python] LDA处理文档主题分布代码入门笔记 http://blog.csdn.net/eastmount/article/details/50824215
- Python静态代码检查工具Flake8
简介 Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强.Flake8是对 ...
- 孤荷凌寒自学python第三十二天python的代码块中的异常的捕获
孤荷凌寒自学python第三十二天python的代码块中的异常的捕获 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天简单了解了Python的错误陷阱,了解到其与过去学过的其它语言非常类似 ...
- Python pep8代码规范
title: Python pep8代码规范 tags: Python --- 介绍(Introduction) 官方文档:PEP 8 -- Style Guide for Python Code 很 ...
- Python 控制流代码混淆简介,加大别人分析你代码逻辑和流程难度
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 王平 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...
- Python一行代码
1:Python一行代码画出爱心 print]+(y*-)**-(x**(y*<= ,)]),-,-)]) 2:终端路径切换到某文件夹下,键入: python -m SimpleHTTPServ ...
- python爬虫代码
原创python爬虫代码 主要用到urllib2.BeautifulSoup模块 #encoding=utf-8 import re import requests import urllib2 im ...
随机推荐
- Jquery Plugins Jquery Validate
Jquery Validate 一.什么是Jquery Validate: jQuery Validate 插件为表单提供了强大的验证功能. 二.常用值: 1 required:true 必须输入 ...
- SKU:唯一标识填什么
策略 随意填写 只要别和别人重复就好 ,不过重复你也创建不了. 最好填与APP信息相关的,比如直接填写bundle ID 上去...跟套装ID保持一致. 你新建应用的时候都还没有APP ID 你怎么填 ...
- Django模板—-自定义过滤器和标签
一.filter和simple_tag 1.在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 2.在app中创建template ...
- 【Spring Boot-技巧】API返回值去除为NULL的字段
简介 在前后端分离的微服务时代,后端API需要良好的规范.本篇主要将一个数据返回时的一个小技巧-- 过滤为空字段 解决痛点:将有效解决数据传输过程中的流量浪费. 组件简介 Jackson Object ...
- Unity---动画系统学习(5)---使用MatchTarget来匹配动画
1. 介绍 做好了走.跑.转弯后,我们就需要来点更加高级的动画了. 我们使用自带动画学习笔记2中的FQVault动画,来控制人物FQ. 在动画学习笔记4的基础上添加Vault动画. 添加一个参数Vau ...
- php常用的系统函数大全
字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...
- 初用sqlite3.exe
1.记得要先建立数据库文件 为了进行数据库的编写,我安装了sqlite3,由于刚接触数据库,我尝试着建立表,并插入元组,属性,用select from语句也可以调出写入的内容,但是不知道如何保存,直接 ...
- 网络瓶颈、线程死锁、内存泄露溢出、栈堆、ajax
网络瓶颈:网络传输性能及稳定性的一些相关元素 线程死锁:多个线程因竞争资源造成的一种僵局 下面我们通过一些实例来说明死锁现象. 先看生活中的一个实例,2个人一起吃饭但是只有一双筷子,2人轮流吃(同时拥 ...
- linux的目录和基本的操作命令
目录相关操作:( ctrl+l 清空当前的屏幕中的命令 ) 一:目录说明: . 当前目录.. 上一层目录- 前一个工作目录~ 当前[用户]所在的家目录 蓝色的文件: 都是目录 白 ...
- LeetCode905.按奇偶排序数组
905.按奇偶排序数组 问题描述 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例 输入:[3,1,2, ...