Python开发【1.1 基础语法】

1.Python语言特点

优点:
①.丰富的库
②.简单、开源
③.支持面向对象编程
④.解释性语言,无需编译
⑤.高层语言,不用考虑内存问题
⑥.可移植性好,不依赖于操作系统


缺点:
①.运行效率较低
②.构架选择过多
③.中文资源较少

2.Python应用场景

应用场景:

①.游戏
②.桌面软件
③.服务器软件
④.WEB应用开发
⑤.早期原型设计、迭代
⑥.操作系统管理,服务器运维
⑦.科学计算(人工智能,数据挖掘与分析)

3、注释

单行注释:# hello python
多行注释:"""hello python"""

 4、中文编码问题

中文编码解决方法:
①.#coding=utf-8
②.#-*- coding:utf-8 -*-

5、多行语句

  Python 通常是一行写完一条语句,但如果语句很长,可以使用反斜杠()来实现多行语句,示例:

total = 'item_one' + 
'item_two' +
'item_three'
print(total)

执行结果:
item_oneitem_twoitem_three

  在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(),示例:

total = ['item_one', 'item_two', 'item_three',
        'item_four', 'item_five']
print(total)

执行结果:
['item_one', 'item_two', 'item_three', 'item_four', 'item_five']

6、标识符

  标识符: 由字母、数字、下划线组成,但不能以数字开头(标识符是区分大小写的

  特殊标识符:

  • 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过提供的接口进行访问,不能用 from xxx import * 而导入;
  • 以双下划线开头的__foo代表类的私有成员;以双下划线开头和结尾的__foo__代表 Python 里特殊方法专用的标识,如__init__()代表类的构造函数。

  Python保留字: 保留字即关键字,不能用作任何标识符名称。Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'if',
'return','try', 'while', 'with', 'yield']

7、format格式化函数

  • 格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

  • 基本语法是通过 {} 和 : 来代替以前的 % 。

  • >>>"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
    'hello world'
    
    >>> "{0} {1}".format("hello", "world")  # 设置指定位置
    'hello world'
    
    >>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
    'world hello world'
    
    >>> print("网站名:{name}, 地址 {url}".format(name="百度", url="www.baidu.com")) #指定参数名
    '网站名:百度, 地址 www.baidu.com'
    
    
    >>>site = {"name": "百度", "url": "www.baidu.com"}
    >>>print("网站名:{name}, 地址 {url}".format(**site)) # 通过字典设置参数
    '网站名:百度, 地址 www.baidu.com' 
    
    >>>my_list = ['百度', 'www.baidu.com']
    >>>print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的 通过列表索引设置参数
    '网站名:百度, 地址 www.baidu.com'
    
    >>> print("{:.2f}".format(3.1415926)); #数字格式化
    3.14

8、输入和输出

 输入:input()

#!/usr/bin/python3

str = input("请输入:");
print ("你输入的内容是: ", str)

>>>程序执行结果:
请输入:Hello Python!
你输入的内容是:  Hello Python! 

 输出:print()

#!/usr/bin/python3

x="a"
y="b"

# 换行输出
print( x )
print( y )

# 不换行输出
print( x, end=" " )
print( y, end=" " )

# 同时输出多个变量
print(x,y)

# 打印值
print ("hello")

# 打印变量
age = 18
print("age变量值是:%d",%age)

实例:

#-*- coding:utf-8 -*-
# 注意:
# input()返回的是字符串
# 必须通过int()将字符串转换为整数
# 才能用于数值比较:

a = int(input("input:"))
b = int(input("input:"))
c = input("input:")
print(type(c))
print(type(a))

print('%d*%d=%d'%(a,b,a*b))

输入:
input:3
input:3
input:2

执行结果:
<class 'str'>
<class 'int'>
3*3=9

9、变量

①.什么是变量:
    变量是指没有固定的值,可以改变的数,功能是存储数据

②.变量的定义:
    等号(=)运算符左边是一个变量名,等号(=)运算符右边是存储在变量中的值
    示例:
    counter = 100 # 整型变量
    miles = 1000.0 # 浮点型变量
    name = "demo" # 字符串

③.多个变量赋值:
    # 创建一个整型对象,值为1,三个变量被分配到相同的内存空间上
    a = b = c = 1
    # 两个整型对象 1 和 2 的分配给变量 a 和 b,字符串对象 "demo" 分配给变量 c
    a, b, c = 1, 2, "demo"

④.变量类型转换
  a = 1
  b = float(a)
  print(b)
  
  >>>1.0
原文地址:https://www.cnblogs.com/loser1949/p/9484866.html