码迷,mamicode.com
首页 > 编程语言 > 详细

python3------基础语法

时间:2018-06-24 23:54:14      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:pytho   tin   浮点   相同   命令   int   5.6   com   转义   

1 注释

 1.1 以#号开头的注释

 1.2 以""" 注释内容 """

 1.3 以 ''' 注释内容'''

2 行与缩进

   python代码块通过缩进对齐表达代码逻辑而不是使用大括号;

   缩进表达一个语句属于哪个代码块。

   语句块缩进(常用4个空格)

3 多行语句

   如果书写的语句很长,可以使用反斜杠(\)来实现多行语句,例如:

   >>> print ("hello \

         world")

         hello world

   注意:在列表[],字典{}或者元组()中的多行语句中不需要使用反斜杠

4 数字类型

   int(整数)

   bool(布尔)

   float(浮点)

   complex(复数)

5 字符串

 5.1 python中单引号和双引号使用完全相同

 5.2 使用转义字符 \

 5.3 使用r可以让反斜杠不发生转义

       >>> print ("this is a string \n")

       this is a string 


       >>> print (r"this is a srting \n")

       this is a srting \n

 5.4 python中字符串有两种索引的方式,从左往右以0开始,从右往左以-1开始

 5.5 字符串截取

       #!/usr/bin/python
       str = 'hello world'
       print (str)                #输出字符串 hello world       

       print (str[0:-1])       #输出第一个到倒数第二个 hello worl

       print (str[0])           #输出第一个字符  h       

       print (str[2:5])        #输出从第三个开始到第五个字符 llo

       print (str * 2)         #输出字符串2次 hello worldhello world

       print (str + 'add')  #连接字符串 hello worldadd


 5.6 等待用户输入 input

       >>> input ("please input:")

       please input:hello world

       'hello world'

6 import 与 from ... import

   import:将整个模块导入

   from ... import :从某个模块中导入函数

7 命令行参数

  # python -h 

  

python3------基础语法

标签:pytho   tin   浮点   相同   命令   int   5.6   com   转义   

原文地址:http://blog.51cto.com/13452945/2132286

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!