01Python入门

Python入门

本专题的目标

  1. 掌握基本的Python语法,编写简单的py程序
  2. 编写简单的爬虫
  3. 基本的数据分析

Python的特性

  1. 语法简单,容易理解
  2. 跨平台
  3. 有强大、丰富的第三方库
  4. 作用:网站、爬虫、大数据处理、机器学习……
  5. 动态类型语言

Hello World

import this

print("Hello")
print("World")

# 输出:
# Beautiful is better than ugly.
# Explicit is better than implicit.
# Simple is better than complex.
# Complex is better than complicated.
# Flat is better than nested.
# Sparse is better than dense.
# Readability counts.
# Special cases aren't special enough to break the rules.
# Although practicality beats purity.
# Errors should never pass silently.
# Unless explicitly silenced.
# In the face of ambiguity, refuse the temptation to guess.
# There should be one-- and preferably only one --obvious way to do it.
# Although that way may not be obvious at first unless you're Dutch.
# Now is better than never.
# Although never is often better than *right* now.
# If the implementation is hard to explain, it's a bad idea.
# If the implementation is easy to explain, it may be a good idea.
# Namespaces are one honking great idea -- let's do more of those!
# Hello
# World

Python初体验

计算器:

  • ±*/
  • **:乘方
  • import math
import math

print(3 ** 2)
print(math.pi)

print('---------')

price = 5
count = 10
cost = price * count
print("count: {}".format(count))

# 输出:
# 9
# 3.141592653589793
# ---------
# count: 10

规范和变量

命名规范

  • 与Java(驼峰命名)不同,使用_分割

  • 第一个字符必须是字母或者_

值的交换:

import math

a = 10
b = 20
# 交换a, b的值
a, b = b, a
print("a = {}, b = {}".format(a, b))

print("-----")

a = 10
b = 20
c = 30
# 交换a, b, c的值
a, b, c = b, c, a
print("a = {}, b = {}, c = {}".format(a, b, c))

print("-----")

# 取两位小数
print(round(math.pi, 2))  

# 输出:
# a = 20, b = 10
# -----
# a = 20, b = 30, c = 10
# -----
# 3.14

代码规范

  • 不用单个字母取变量名
  • 不用拼音首字母取变量名!!!
  • 清晰表达变量的含义
  • 必要时加注释
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值