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

Python 基础一:基本语法:字符串---数字----布尔

时间:2019-01-08 17:18:02      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:shandong   cannot   数据类型   false   需要   字符串拼接   逻辑   错误   mes   

数字类型:int(整型)

  定义:

       查找数据类型:type

                        

  >>> a= 2**64
  >>> type(a)  #type()是查看数据类型的方法
  <type ‘long‘>
  >>> b = 2**60
  >>> type(b)
  <type ‘int‘>

字符串:

  1、定义:在Python中,加了引号的字符都被认为是字符串!

  2、使用规则:

  >>> name = "Alex Li" #双引号
  >>> age = "22"       #只要加引号就是字符串
  >>> age2 = 22         #int
  >>> 
  >>> msg = ‘‘‘My name is Alex, I am 22 years old!‘‘‘  #我擦,3个引号也可以
  >>> 
  >>> hometown = ‘ShanDong‘   #单引号也可以

    单双引号没有任何区别,只有下面这种情况 你需要考虑单双的配合

      如:msg = "My name is Alex , I‘m 22 years old!"

    

 

    多引号的作用:就是多行字符串必须用多引号

     :msg = ‘‘‘
        今天我想写首小诗,
        歌颂我的同桌,
        你看他那乌黑的短发,
        好像一只炸毛鸡。
        ‘‘‘

        print(msg)    
  3、字符串拼接:只能进行"相加"和"相乘"运算。
    
    如:
         >>> name = ‘Alex Li‘
        >>> age = ‘22‘
        >>>
        >>>
message = name + age

        >>> name + age #相加其实就是简单拼接
        ‘Alex Li22‘
        >>> 
        >>> name * 10 #相乘其实就是复制自己多少次,再拼接在一起
        ‘Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li‘
    
     
           注:注意,字符串的拼接只能是双方都是字符串,不能跟数字或其它类型拼接
                
   
          如:
                >>> type(name),type(age2)
                (<type ‘str‘>, <type ‘int‘>)
                >>> 
                >>> name
                ‘Alex Li‘
                >>> age2
                22
                >>> name + age2
                Traceback (most recent call last):
                 File "<stdin>", line 1, in <module>
                TypeError: cannot concatenate ‘str‘ and ‘int‘ objects #错误提示数字 和 字符 不能拼接

布尔类型:

  定义:就两个值 ,一个True(真),一个False(假), 主要用作逻辑判断

          详解: 

      如:a = 2,b = 5       a > b     不成立    #  计算机中怎么表示这个不成立呢?

                使用布尔类型来表示:

            >>> a=3

               >>> b=5

              >>> 
              >>> a > b #不成立就是False,即假
              False
              >>> 
              >>> a < b #成立就是True, 即真
              True

 

          

        

 

Python 基础一:基本语法:字符串---数字----布尔

标签:shandong   cannot   数据类型   false   需要   字符串拼接   逻辑   错误   mes   

原文地址:https://www.cnblogs.com/jcchongshi/p/10239863.html

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