美文网首页
基本使用

基本使用

作者: 小董不太懂 | 来源:发表于2019-03-19 20:46 被阅读0次

本文参考:https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/

python中print函数用法总结。

Python 思想:“一切都是对象!”
在 Python 3 中接触的第一个很大的差异就是缩进是作为语法的一部分,这和C++等其他语言确实很不一样,所以要小心 ,python3缩进要使用4个空格(这不是必须的,但你最好这么做),缩进表示一个代码块的开始,非缩进表示一个代码的结束。没有明确的大括号、中括号、或者关键字。这意味着空白很重要,而且必须要是一致的。第一个没有缩进的行标记了代码块,意思是指函数,if 语句、 for 循环、 while 循环等等的结束。
输入很简单:

>>> x=input('please input x:')
please input x:

输出的print函数总结:

1. 字符串和数字类型

可以直接输出:

please input x:
>>> print('hello world')
hello world
>>> print(123)
123

2. 变量

无论什么类型,数值,布尔,列表,字典...都可以直接输出:

>>> x='hello world'
>>> print(x)
hello world
>>> x=(3>2)
>>> print(x)
True
>>> L = [1,2,'a']
>>> print(L)
[1, 2, 'a']
>>> t = (1,2,'a')
>>> print(t)
(1, 2, 'a')
>>> d = {'a':1, 'b':2}
>>> print(d)
{'a': 1, 'b': 2}

3. 格式化输出

类似于C中的 printf

>>> s='hello'
>>> x=len(s)
>>> print('The length of %s is %d'%(s,x))
The length of hello is 5

格式化很多时候用Format函数执行,Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。基本语法是通过 {} 和 : 来代替以前的 % 。format 函数可以接受不限个参数,位置可以不按顺序。

>>> print('The length of {0} is {1}'.format('hello','5'))
The length of hello is 5
>>>"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
'hello world'
 
>>> "{0} {1}".format("hello", "world")  # 设置指定位置
'hello world'
 
>>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
'world hello world'

也可以设置参数:

>>> print("网站名:{name}, 地址 {url}".format(name="东方AV在线", url="www.197ww.com")) #男生福利,你们懂滴
网站名:东方AV在线, 地址 www.197ww.com

4.如何让 print 不换行

在Python中总是默认换行的,比如:

>>> for x in range(0,10):
...     print(x)          #记得这行需要tab一下哦
...
0
1
2
3
4
5
6
7
8
9

在这里我们用help查看一下print()函数:

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.

显然,我们平时只是输入了value参数,而自动忽略了其他参数。

>>> for x in range(0,10):
...     print(x,end="")
...
0123456789

5. 字符串的拼接

>>> print("hello"+" " +"world")
hello world
>>> print("apple"+8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print('apple'+str(8))
apple8
>>> print('1+2')
1+2
>>> print(int(1+2))
3

我们考虑如何输出 I'm ok!

>>> print("I'm ok!")
I'm ok!
>>> print('I'm ok!')
  File "<stdin>", line 1
    print('I'm ok!')
             ^
SyntaxError: invalid syntax
>>> print('I\'m ok! ')
I'm ok!

相关文章

  • Flutter--Text/Container/Image

    Text基本使用 Container基本使用 Image基本使用

  • 基本使用

    1、 打开需要上传的文件夹执行: git init 格式化窗口 2、执行 git add . 上传文件 3、执行 ...

  • 基本使用

    href="javascript:;" 其中javascript: 是一个伪协议。它可以让我们通过一个链接来调用...

  • 基本使用

    数据库: 什么是数据库?简单来说就是存数据的。 都有什么是数据库? oracle(强大,跟金融政府打交道的,安全,...

  • 基本使用

    本文参考:https://morvanzhou.github.io/tutorials/machine-learn...

  • 6-xpath和css select基本使用

    Xpath基本使用 css select基本使用

  • MySQL语法入门(一)

    MySQL语法入门(一) 基本运算符使用 基本数学函数使用 基本字符串函数使用 基本日期时间函数使用

  • python time与datetime模块基本使用

    time模块基本使用 datetime模块基本使用

  • SQL语句基本使用

    SQL语句基本使用——增删改查 SQL语句基本使用——WHERE子句 SQL语句基本使用——AND和OR的使用 S...

  • iOS-UICollectionView基本使用

    iOS-UICollectionView基本使用 iOS-UICollectionView基本使用

网友评论

      本文标题:基本使用

      本文链接:https://www.haomeiwen.com/subject/chdnmqtx.html