Python语言数据类型
小标 2018-09-20 来源 : 阅读 1243 评论 0

摘要:本文主要向大家介绍了Python语言数据类型,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了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]    

以上就介绍了Python的相关知识,希望对Python有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程