python介绍

1.python介绍

编程语言类型

编译型

解释型

静态语言

动态语言

强类型定义语言

弱类型定义语言

2.python基础

2.1第一条编辑语言

print("Hello, Python")
print("To be, or not to be,it's up to you.")
print("Make it!")
print("以梦为马,不负韶华!")

2.2print()函数

作用:在控制台打印信息;

例子:

> > > help(print)

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
注释
单行注释

一个“#”

# 这是单行注释
多行注释

三单引号

三双引号

'''
多行注释1
'''


"""
多行注释2
"""

demo:

print("Hello, Python")    #我叫丁义阔
print("To be, or not to be,it's up to you.")
print("Make it!")
print("以梦为马,不负韶华!")
 '''
 python
 '''

2.3input()函数

作用:从外部获取值

用法:括号中可以提供提示信息

返回值为字符串类型

>>> a=input()
城市学院
>>> a
'城市学院'
>>> a=input("Please Enter School Info:")
Please Enter School Info:City College
>>> a
'City College'

2.4运行python程序的三种方式

(1)解释器环境运行
(2)脚本运行
(3)IDE

pycharm

sublime

VS Code

Atom

2.5Python中常见的数据类型

数据的概念

Python中的数据类型(常见)

  • 数字(number)

整形(int)

浮点型(float)

布尔型(bool)

复数(complex)

  • 字符串(string)

  • 列表(list)

  • 元组(tuple)

  • 字典(dict)

  • 集合(set)

2.6变量和常量

变量

概念:

程序可操作的存储区的名称;

程序运行时存储区中能够改变的数据;

每个变量都有自己的特定的数据类型

作用:

将数据存储到内存

变量的定义:

命名

规则:

必须是字母、下划线、数字组合

不能以数字开头

不可以是Python中的关键字

注意:

见名知意

区分大小写

在Python中,单下划线和双下划线开头的变量有特殊的通途,一般不推荐使用单下划线和双下划线开头的变量名称

变量定义

val_name = 初始值

拓展

关键字的查看

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

类型测试type()

type(val_name)		

内存地址id()

id(val_name)
常量

不变的量

pi, 3.1415926

通常用大写,如

PI=3.1415926

举例:

>>> a=1
>>> b=6.6
>>> c="xyz"
>>> tp=1,2,3
>>> x,y,z=tp
>>> x
1
>>> y
2
>>> z
3
>>> type(tp)
<class 'tuple'>

互换量元素的值

2.7数字

整型(int)

32bit,取值范围$ -2^{31}- 2^ {31}-1$

64bit,取值范围 − 2 63 − 2 63 − 1 -2^{63}-2^{63}-1 2632631

以前python区分短整型和长整型,目前,无这些区分。

浮点型(float)

带有小数点的数字

布尔型(bool)

True

False

复数(complex)

实部(cpx.real)

虚部(cpx.imag)

共轭复数(cpx.conjugate())

>>> cpx=complex(3,4)
>>> cpx
(3+4j)
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c', 'cpx', 'keyword', 'tp', 'x', 'y', 'z']
>>> dir(cpx)
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']
>>> cpx.real
3.0
>>> cpx.imag
4.0
>>> cpx.conjugate()
(3-4j)
常见的数学函数
函数名描述
abs(x)绝对值
pow(x,y) x y x^y xy
round(x,[n])返回四舍五入的值,如果给出n,表明四舍五入后的小数点的位数
math.exp(x)返回e的x次幂
math.exp(x)返回绝对值,类型为浮点数
math.ceil(x)返回数字的上入整数
math.floor(x)返回数字的下舍整数
math.log(x)单参数,默认以e为底;双参数,以[y]为底
math.modf(x)返回浮点数的小树部分和整数部分
math.sqrt(x)平方根
m a x ( x 1 , x 2 , . . . ) max(x_1,x_2,...) max(x1,x2,...)返回最大值
m i n ( x 1 , x 2 , . . . ) min(x_1,x_2,...) min(x1,x2,...)返回最小值

2.8表达式与运算符

  • 表达式

    • 由变量,常量和运算符组成的式子,成为表达式
  • 运算符

    • 算术运算符
    • 比较运算符
    • 逻辑运算符
    • 赋值运算符
    • 成员运算符
    • 身份运算符
    • 位运算符
    Python算术运算符
    运算符描述实例
    +相加/拼接a+b
    -相减/求差集a-b
    *相乘/重复a*b
    /相除a/b
    **幂运算a**b
    %取模(余数)a%b
    //取整除(商)a//b

拓展

divmod(x,y)

返回x除以y得到的商和余数(以元组形式)

>>> divmod(23,4)
(5, 3)
>>>
Python比较运算符
Python赋值运算符
>>> a=1
>>> a+=1
>>> a
2
>>> a*=3
>>> a
6
>>>
Python位运算符

按位运算符是把数字看做二进制来计算的。常见的位运算符如下:

运算符描述
&按位“与”,同为1才为1,否则为0
|按位“或”,有1即为1
^按位“异或”,不同为1
~按位“取反”,~x得到-x-1
<<左移运算符
>>右移运算符
a=15
b=31

二进制分别对应:
a--->0000 1111
b--->0001 1111
a&b  0000 1111
a|b  0001 1111
a^b  0001 0000

a>>2 0000 0011
a<<2 0011 1100
python成员运算符

测试是否包含某个成员,一般测试字符串,列表,元组

运算符描述
in如果存在指定序列中,返回True;否则,False
not in如果不存在指定序列中,返回True;否则,False
>>> sr="abc"
>>> "a"in sr
True
>>> "a" not in sr
False
>>> li=list(sr)
>>> li
['a', 'b', 'c']
>>> "a"in li
True
python身份运算符

id()

运算符描述
is判断两个标识是不是引用自同一个对象
not is判断两个标识是不是引用自同一个对象
Python逻辑运算符

and

or

not

2.9数据类型的强制转换

强转类型描述
int(x,[base])将强制转化为整型。如果x是字符串,需要制定base基数
float(x)将x转换为浮点型
str(x)强转字为字符串
repr()将对象转换为表达式字符串
list(x)
tuple(x)
dict(x)
set(x)
chr(x)将整数转化为字符
ord(x)将字符转化为数值
bin(x)转换为二进制
oct(x)转换为八进制
hex(x)转换为十六进制
>>> a=123
>>> complex(a)
(123+0j)
>>>
>>> a=123
>>> b=str(a)
>>> type(b)
<class 'str'>
>>> chr(65)
'A'
>>> sr="123"
>>> int(sr)
123
>>> int(sr,8)
83
>>> 8**2+2*8+3
83
>>> int(sr,16)
291
>>> ord("城")
22478
>>> bin(15)
'0b1111'
>>> oct(15)
'0o17'
>>> hex(15)
'0xf'
>>>

2.10随机函数

导入这个模块

import random

random模块的常见功能:

(1)random.random()

  • 用来生成一个0-1的随机浮点数[0,1)

    import random
    # print(dir(random))
    for i in range(100):
        a = random.random()
        print(a,end=" ")

(2)random.uniform(a,b)

  • 用来生成指定范围内的浮点数[a,b]

    import random
    # print(dir(random))
    for i in range(100):
        a = random.uniform(6,8)
        print(a,end=" ")

(3)random.randint(a,b)

  • 用来生成指定范围内的整数[a,b]

    import random
    # print(dir(random))
    for i in range(100):
        a = random.randint(610)
        print(a,end=" ")

(4)random.randrange([start],stop,[step])

  • 从指定范围内,按指定基数递增或者递减的集合中,随机获取一个整数

    random.randrange(1,16,2)
    1,3,5,7,9,11,13,15

    import random
    for i in range(100):
        a = random.randrange(116,2)
        print(a,end=" ")

(5)random.choice(sequence)

  • 从序列中随机获取一个元素,sequence可以为列表,字符串或者元组

    import random
    for i in range(100):
        b = random.choice(range(1,16,2))
        print(b,end=" ")

(6)random.shuffle(x,[random])

  • 用来将一个列表的元素打乱,即将列表的元素随机排列

    import random
    li=["c Language","C++","Java","VB","C#","Python"]
    for i in range(6):
        random.shuffle(li)
        print(li)

(7)random.sample(sequence,k)

  • 确定序列中随机获取指定长度的片段并随机排列。但不会修改原来序列
import random
li=["c Language","C++","Java","VB","C#","Python"]
for i in range(6):
    print(random.sample(li,4))
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值