【python零基础入门学习】python基础篇(一)

python基础学习

官方: www.python.org,自行安装,linux上有自带python,win自行安装。

[student@room9pc01 05]$ python --version

Python 2.7.5

 #创建虚拟环境:

[root@room9pc01 bin]# pwd

/root/nsd1907/bin

[root@room9pc01 bin]# ls

activate  activate.csh  activate.fish  easy_install  easy_install-3.6  pip  pip3  pip3.6  python  python3

[root@room9pc01 bin]#

[root@room9pc01 ~]# python3 -m venv ~/nsd1907

#激活虚拟环境:

[root@room9pc01 ~]# source ~/nsd1907/bin/activate

(nsd1907) [root@room9pc01 ~]# python --version

Python 3.6.7

(nsd1907) [root@room9pc01 ~]# which python

/root/nsd1907/bin/python

基本操作:

Ctrl + / : 加或者去掉注释
Ctrl + d 或者 exit() 退出python
shift + Tab :减去缩进(可以选中多行)

#打印一行字符串
print("yyf","xixi")

print("wogan" 
      "qude")

#字符串可以使用 + 进行拼接, 拼接后再打印  ---''里面的类型会全部转化为字符类型
print("hello " + "world")
print('hao' + '123')
#print打印多项时,用逗号分开各项,默认各项间使用空格作为分隔符
print('hao', 123)

#也可以通过sep制定分隔符----python3独有的语法
print('hao', 123, 'abc', 456)
print('hao',123,'abc',456,sep='***')
a=1
b=2
c=a+b
print(c)

#字符串中间如果需要有变化的内容,可以使用%s占位,然后再字符串外边指定具体内容

print('I am %s' % 'zzg') print('%s is %s years old.' % ('tom' , 20))

#输入语句input()
username = input('username: ')
print(username)

username = input('username: ')
print('welcome',username)
print('welcome ' + username)
print('welcome %s' % username)

#注意:input读入的内容一定是字符类型
n = input('number: ')
#print(n + 10) #错误, 不能把字符和数字进行运算
a = int(n) + 10 #int函数可以将字符串数字转换成整数
print(a)

测试结果:
/root/nsd1907/bin/python /root/nsd1907/py01/day01/py2.py
yyf xixi
woganqude
hello world
hao123
hao 123
hao 123 abc 456
hao***123***abc***456
3
I am zzg
tom is 20 years old.
username: tom -----(自己手动输入)
tom
username: yyf-----(需要手动输入)
welcome yyf
welcome yyf
welcome yyf
number: 10
20
1010

变量:

  • 会变化的量, 与之对应的是"字面量", 也就是字面本身的含义 (常量:不会变化的量)

  • 变量使用方便----重复使用或者需要更改的时候

  • 变量可以使用有意义的名字---方便管理,之后的读取

变量命名的约定:

  • 首字符必须是字母或者下划线

  • 后续字符可以是字母,数字或者下划线

  • 区分大小写

  • 变量在使用之前,必须先初始化赋值,否者将出现名称错误

推荐的命名方式:

  • 变量名全部采用小写字母 pythonstring

  • 简短,有意义 pystr

  • 多个单词间用下划线分隔 py_str

  • 变量名用名词,函数名用谓词(动词+名词)phone update_phone

  • 类名采用驼峰形式,MyClass

变量赋值:

  • 变量赋值操作,是自右向左进行的,即,将=右边的表达式计算出结果后,赋值给左边的变量

>>n = 5

>>n = n + 1

>>n

>>6

n+=1  等于 n = n + 1

不支持i++

运算符:

例如 + - * / // % **

>>> 5/3

1.6666666666666667

>>> 5//3

1

>>> 5%3  ---求余数,求模

2

>>> divmod(5,3)---同时得到商和余数

(1, 2)

>>> a , b = divmod(5,3)

>>> a

1

>>> b

2

>>> 2 ** 3 -- 2的3次方

8

比较运算符:

得到的结果为真Ture或者假False

例如  > >= < <= == !=

>>>

>>>

>>>

>>> 3 == 3

True

>>> 3 != 3

False

>>> 3 != 4

True

>>> 10 < 15 < 20----python支持连续比较

True

>>> 10 < 15 > 13-----相当于10 < 15 and 15 > 13

True

逻辑运算符:

and

or

not ----优先级最高

>>> 5 > 3 and 5 < 7  同时成立

True

>>> 5 > 10 and 3 < 2

False

>>> 5 > 10 or 10 > 2  一边成立即可

True

>>> not 5 > 30   ---取反

True

数据类型概述:

数字:

>>> 11

11

>>> 0o11   ---1* 8的一次方 + 1* 8的0次方

9

>>> 0x11   ---1* 16的一次方 + 1* 16的0次方

17

>>> 0b11

3

>>> oct(20)  -转8进制

'0o24'

>>> hex(20)  -转16进制

'0x14'

>>> bin(20)  -转2进制

'0b10100'

10000秒化为时分秒

>>> divmod(10000,60)

(166, 40)

>>> divmod(166,60)

(2, 46)

字符串:

定义字符串:

>>> words = """

... xixi

... xiix

... xixixi

... """

>>> print(words)

xixi

xiix

xixixi

>>> words  

'\nxixi\nxiix\nxixixi\n'

在python解释器中,直接写变量,将会输出内部存储的样式

字符串切片:

>>> py_str = 'python'

>>> py_str[0]

'p'

>>> py_str[-1]

'n'

>>> py_str[-2]

'o'

>>> py_str[4]

'o'

>>> py_str[2:4]

'th'

>>> py_str[2:] ----或者[2:2000000]比下标范围大即可

'thon'

>>> py_str[:4]

'pyth'

>>> py_str[::2]-----步长值为2  偶数 ---开头默认为0

'pto'

>>> py_str[1::2]

'yhn'

>>> len(py_str) ---算长度

6

>>> 'py' in py_str

True

>>> 'to' in py_str---需要连续

False

字符串的拼接:

>>> c = 'is me'

>>> py_str + '' + c

'pythonis me'

>>> py_str + ' ' + c

'python is me'

>>> '*' * 10

'**********'

>>> py_str * 3

'pythonpythonpython'

列表:

>>> alist = [10 , 20 , 'tom' , 'yyf' , [1,2]]

>>> len(alist)

5

>>> 20 in alist

True

>>> alist[-1]

[1, 2]

>>> alist[2:4]

['tom', 'yyf']

>>> alist + [100]

[10, 20, 'tom', 'yyf', [1, 2], 100]

>>> alist * 2

[10, 20, 'tom', 'yyf', [1, 2], 10, 20, 'tom', 'yyf', [1, 2]]

>>> alist[-1]=50

>>> alist

[10, 20, 'tom', 'yyf', 50] ---修改内容

>>> c = alist + [100]

>>> c

[10, 20, 'tom', 'yyf', 50, 100]

>>> alist.append('shachao')  ----增加列表数值

>>> alist

[10, 20, 'tom', 'yyf', 50, 'shachao']

>>> alist.

alist.append(  alist.count(    alist.insert(  alist.reverse( 

alist.clear(    alist.extend(  alist.pop(      alist.sort(   

alist.copy(    alist.index(    alist.remove(

元组:

  • 与列表类似---"静态"的列表

  • 可以认为元组是不可变的列表--不能修改内容

>>> atuple = (10 , 20 ,'yyf')

>>> atuple

(10, 20, 'yyf')

>>> atuple[0]

10

>>> atuple[2:3]

('yyf',)

>>> atuple[1:3]

(20, 'yyf')

>>> alist[1:3]

[20, 'tom']

字典:

>>> adict = {'name': 'yyf' , 'age': 20}

>>> len(adict)

2

>>> adict['name'] #通过key取出value

'yyf'

>>> 'name' in adict --'name'是字典的key吗?

True

>>> adict['age'] = 22 # 已有键,改值

>>> adict['email'] = 'qq@163.com' #没有键,添加新值

>>> adict

{'name': 'yyf', 'age': 22, 'email': 'qq@163.com'}

数据类型比较:

 ---数据是存储在内存里面.

[]:列表

():元组

{}:字典

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值