python数据类型

释放双眼,带上耳机,听听看~!

python数据类型

运行python文件的时候,python会通过编译器将它编译成.pyc文件。
如果没有修改python文件,每次执行程序时,就执行前面运行的程序,不需要重新编译。

字符串类型,使用单引号,或者双引号包围,是由零个或者多个字符串组成的有限串行。

>>> print("what's your name");
what's your name
>>> print("what/'s your name");
what's your name
>>> 'hello'+"world";
'helloworld'
>>> 'i am '+str(66);
'i am 66'

>>> print(" nice you to /n you");
 nice you to 
 you

>>> print('how do /
you /
do');
how do you do

>>> help(input);
Help on built-in function input in module builtins:

通过iput()函数输入数据,还回是字符串类型。

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.

    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.

    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.

>>> name=input("input your name:")
input your name:dflx
>>> name
'dflx'
>>> type(name);
<class 'str'>

求2数之和

print("please input two number");
a=input("input a=");
b=input('input b=');
print("sum=",(int(a)+int(b)));

=================== RESTART: C:/Users/dflx/Desktop/add.py ===================
please input two number
input a=2
input b=3
sum= 5

原始字符串,是指字符串里面的每一个字符都是原始含义,如反斜杠,不会被看成转义字符。可以r表示开头的字符串。

>>> print(r"c;/nl");
c;/nl
>>> print("c;/nl");
c;
l

string有下标index,可以取出字符,index可以索引字符的下标。

>>> st='hello python';
>>> st[0];
'h'
>>> st.index("h");
0
>>> st[0:3];
'hel'
>>> st
'hello python'
>>> type(st[1]);
<class 'str'>

<class 'str'>
>>> st[:5];
'hello'
>>> st[0:5];
'hello'
>>> st[:-1];
'hello pytho'

len():求序列长度。
+:连接2个序列
*:重复序列的元素
In:判断元素是否存在序列中。
max():返回最大值。
min()换回最小值。

>>> 'e' in a;
True
>>> 'e' in b;
False
>>> max(c);
'w'
>>> min(c);
' '
>>> len(c);
11

ord函数可以得到一个字符对应的编码。

>>> help(ord);
Help on built-in function ord in module builtins:

ord(c, /)
Return the Unicode code point for a one-character string.

>>> ord(' ');
32
>>> ord('w');
119

>>> a='py';
>>> a*3;
'pypypy'
>>> help(len);
Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

>>> help(min);
Help on built-in function min in module builtins:

min(...)
    min(iterable, *[, default=obj, key=func]) -> value
    min(arg1, arg2, *args, *[, key=func]) -> value

    With a single iterable argument, return its smallest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
With two or more arguments, return the smallest argument.

字符串的格式输出。
format() 格式化函数

>>> help(format);
Help on built-in function format in module builtins:

format(value, format_spec='', /)
    Return value.__format__(format_spec)

format_spec defaults to the empty string

>>> "hello{0:>8} you are {1:6} i {2:5} you".format("dflx",'great','like');
'hello    dflx you are great  i like  you'
>>> "dflx age id {0:d}  and height {1:.2f}".format(999,1.7266);
'dflx age id 999  and height 1.73'

>>> help(str.isalpha);
Help on method_descriptor:

字符串相关函数

isalpha(...)
    S.isalpha() -> bool

    Return True if all characters in S are alphabetic
    and there is at least one character in S, False otherwise.

>>> "pythonn".isalpha();
True
>>> "py6".isalpha();
False

>>> help(str.split);
Help on method_descriptor:

split(...)
    S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
removed from the result.

>>> url="www.baidu.com";
>>> url.split(".");
['www', 'baidu', 'com']
>>> url.split(" ");
['www.baidu.com']

strip();去掉字符串二端的空格。
lstrip(); 去掉字符串左边的空格。
rstrip() 去掉右边的空格。

NameError: name 'strip' is not defined
>>> help(str.strip);
Help on method_descriptor:

strip(...)
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
If chars is given and not None, remove characters in chars instead.

>>> a="   dflx  good  ";
>>> a
'   dflx  good  '
>>> a.strip();
'dflx  good'
>>> a.lstrip();
'dflx  good  '
>>> a.rstrip();
'   dflx  good'

列表,在python中用[]括号表示,在方括号里面的可以是数字,字符串,boolean等其它类型的对象。

>>> a=[2,3];
>>> type(a);
<class 'list'>

>>> df=[1,2,6,'good',"mooc",true];
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> df=[1,2,6,'good',"mooc",True];
>>> print(df);
[1, 2, 6, 'good', 'mooc', True]
>>> df[2];
6
>>> df[2:4];
[6, 'good']
>>> df[-1];
True
>>> df[-3:-1];
['good', 'mooc']

反转

>>> df[: : -1];
[True, 'mooc', 'good', 6, 2, 1]
>>> df[: : -2];
[True, 'good', 2]
>>> list(reversed(df));
[True, 'mooc', 'good', 6, 2, 1]

【转自慕课】https://www.imooc.com

Python

Python操作MySQL]银行转账实例-代码流程

2022-3-3 16:54:40

Python

hashlib.md5()函数来筛选出系统重复文件并移除...

2022-3-3 16:58:06

搜索