Python基础学习!基本元素,数字,字符串以及变量

版权声明:转载请声明原文链接地址,谢谢! https://blog.csdn.net/weixin_42859280/article/details/84593706

判断数据类型:

>>> type(a)
<class 'str'>
>>> b =5
>>> type(b)
<class 'int'>
>>> c= 2.3
>>> type(c)
<class 'float'>
>>>

在这里插入图片描述

除法:

一个/符号得到的是浮点数
一个//符号得到的是整数

在这里插入图片描述
>>> 5/3
1.6666666666666667
>>> 5//3
1
>>>

同时得到商和余数:

>>> divmod(9,5)
(1, 4)
>>>

在这里插入图片描述
等价于分别计算:

>>> 9//5
1
>>> 9%5
4
>>>

在这里插入图片描述
类型转换:

>>> int(True)
1
>>> int(6.7)
6
>>> float(5.0)
5.0
>>>

在这里插入图片描述
Python处理超级大的数,也不会报错。这个是它的一个优点!
字符串:
支持一对或者三对单引号或者双引号! 错误实例:四个双引号就不可以!

>>> 'abcd'
'abcd'
>>> "abcd"
'abcd'
>>> '''abcd'''
'abcd'
>>> """abcd"""
'abcd'
>>> """"abcd""""
  File "<stdin>", line 1
    """"abcd""""
               ^
SyntaxError: EOL while scanning string literal
>>>

在这里插入图片描述
当然,三个引号自然有另外的功能:

>>> a='''
... a
... b
... c
... d
... e
... '''
>>> a
'\na\nb\nc\nd\ne\n'  #已经出现了换行符号!输出就会自动换行!
>>> print(a)

a
b
c
d
e

>>>

在这里插入图片描述
字符串加在一起:
str():可以将数字类型转换成为字符串类型!

>>> str(88)
'88'
>>>

在这里插入图片描述
可以用下面这个创建空字符串:(不过我感觉没啥用呀!)
提前定义一下,即使为空。定义以后才可以使用!
在这里插入图片描述完整的:

>>> b += 'sun '
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
>>> b= ''
>>> b += 'sun '
>>> b += 'is '
>>> b += 'hot '
>>> b
'sun is hot '
>>>

在这里插入图片描述

转义符:
这个跟C语言类似;

  • \n
  • \t
  • \
>>> print('a\nb\nc')
a
b
c
>>> print('a\nb\tc')
a
b       c
>>> print('a\tb\tc')
a       b       c
>>> print('a\\b\tc')
a\b     c
>>> print('a\\nb\tc')
a\nb    c
>>> print('a\\tb\tc')
a\tb    c
>>>

在这里插入图片描述
+符号可以拼接字符串:

>>> a = 'byebye'
>>> b = 'you'
>>> a+b
'byebyeyou'
>>>

在这里插入图片描述
*可以复制字符串:

>>> c = 'you'
>>> f = c*4
>>> f
'youyouyouyou'
>>>

在这里插入图片描述
[]提取字符:
关于偏移量,第一个就是0.最后一个是-1,倒数第二个是-2 etc.

>>> yy = 'abcdef'
>>> yy[0]
'a'
>>> yy[1]
'b'
>>> yy[-1]
'f'

在这里插入图片描述
replace替换指定位置字符:
.replace(‘a’,‘b’,‘n’)
a:表示被替换的字符 b:用来替换的字符 n:替换的次数(默认为1)
要特别注意替换与被替换的字符里的空格。否则会有大问题!

>>> yy = 'abcdef'
>>> yy.replace('a','s')
'sbcdef'
>>>

在这里插入图片描述
会替换对应的字符位置的信息!

[start :end:step]分片:
偏移量定义和前面说的一样。

[start :end:step]分片:
     - [:] 提取开头到结尾的整个字符串
     - [start:]从start提取到结尾
     - [:end] 从开头提取到end-1
     - [start:end] 从start提取到end
     - [start:end:step] 从start提取到end,每step提取一个!

代码:

>>> ss = 'abcdefhijklmnopqrstuvwxyz'
>>> ss [:]
'abcdefhijklmnopqrstuvwxyz'
>>> ss [20:]
'vwxyz'
>>> ss [3:]
'defhijklmnopqrstuvwxyz'
>>> ss [4:12]
'efhijklm'
>>> ss [-4:]
'wxyz'
>>> ss [18:-4]
'tuv'
>>> ss [-6:-4]
'uv'
>>> ss [::6]
'ahntz'
>>> ss [12:-1:6]
'nt'
>>>

在这里插入图片描述
提取全表:

>>> ss [-1::-1]
'zyxwvutsrqponmlkjihfedcba'
>>>

对待错误数据的容忍:

  • 小于起始位置的偏移量,会被当做0对待
  • 大于终止位置的偏移量,会被当做-1对待
>>> ss[-100:-1]
'abcdefhijklmnopqrstuvwxy'
>>> ss[0:80]
'abcdefhijklmnopqrstuvwxyz'
>>>

len():获取字符串长度:

>>> len(ss)
25
>>> r = ''
>>> len(r)
0
>>>

split():分割。
通过一个标志性的字符,或者逗号
将字符串分割成为多个字符串

>>> w = '123,we,re,ty,yu,er,23,rty,yy6u'
>>> w
'123,we,re,ty,yu,er,23,rty,yy6u'
>>> w.split(',')
['123', 'we', 're', 'ty', 'yu', 'er', '23', 'rty', 'yy6u']
>>> w.split('3')
['12', ',we,re,ty,yu,er,2', ',rty,yy6u']
>>>

join():合并字符串。
跟split()函数功能正好相反,这个是将多个字符串分解合并成为同一个字符串!

>>> q = ['12', ',we,re,ty,yu,er,2', ',rty,yy6u']
>>> a =','.join(q)
>>> a
'12,,we,re,ty,yu,er,2,,rty,yy6u'
>>>

熟悉字符串的一些函数:

>>> w = ''' ALL that doth flow we about
... sun is hot
... wo love each other
... peace is very price
... '''
>>> len(w)#获得长度!
79
>>> w[23:]#从第23开始查看!
'about\nsun is hot\nwo love each other\npeace is very price\n'
>>> w.find('is')#查找‘is’字符串在哪里~
33
>>> w.count('is')#统计‘is’,字符串出现的次数!
2
>>>
>>> w.isalnum()#是否全部为数字?
False
>>>

大小写与对齐方式

>>> w = 'today is a sun day... '
>>> w.strip('.')
'today is a sun day... '
>>> w = 'today is a sun day ... '
>>> w.strip('.')
'today is a sun day ... '
>>> w.strip('.')
'today is a sun day ... '
>>> w.capitalize()#字符串首字母变成大写!
'Today is a sun day ... '
>>> w.title()#所有单词开头字母变成大写!
'Today Is A Sun Day ... '
>>> w.upper()#所有字母变成大写!
'TODAY IS A SUN DAY ... '
>>> w.lower()#所有字母变成小写!
'today is a sun day ... '
>>> w.swapcase()#所有字母大小写转换!
'TODAY IS A SUN DAY ... '
>>> w.center(40)#40个字符位,居中
'        today is a sun day ...          '
>>> w.rjust(40)#40个字符位,右对齐
'                 today is a sun day ... '
>>> w.ljust(40)#40个字符位,左对齐
'today is a sun day ...                  '
>>>

列表:
创建列表代码:
在这里插入图片描述

>>> quote = {
... "Moe":"A wise guy,huh?",
... "lua":"OK!",
... "gure":"nice",
... }

测试:
在这里插入图片描述

>>> a="lua"
>>> a = "lua"
>>> print(a,"say",quote[a])
lua say OK!
>>>

这个会对应输出你列表的位置的元素!

猜你喜欢

转载自blog.csdn.net/weixin_42859280/article/details/84593706