Python中的语法规则

1、注释

Python中有逐行注释和部分注释两种

##逐行注释

# this is a command

print ('hello world')

##成段注释

"""
this
is
command
"""
print ('hello world')


##运行结果
[root@contral day01]# python3 command
hello world
hello world

2、变量的命名

2.1 命名方式:

和其它语言相似,变量只能含有数字、下换线、字母,且不能以数字开头,不能和关键字重名;

命名的的方式往往有两种,大头命名法和峰值命名:

##大头命名
FirstName

##峰值命名
firsTName

2.2 基本类型:

命名过程中不进行类型说明,直接命名,命名时的类型可用type类型进行查看。

##字符型 str
name = 'linux'

##整型 int
age = 18

##长整型 long   (该变量只在python2里面有所定义)
date = 1234648965165812352

##浮点型 float
height = 176.5
weight = 64.5

##布尔型 
gender = true / false

bool(gender)  ##有值则为真,无值则为假

##变量定义也可以通过变量复制进行定义
result = weight * 2

2.3 变量的方式:dir(varname)

##可以用dir查看变量的方式
In [5]: string = 'welcome to this world'                                        

In [6]: string                                                                  
Out[6]: 'welcome to this world'

In [7]: dir(string)     ##查看变量的方式                                                        
Out[7]: 
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
....

##方式的使用
In [9]: string.center(40)                                                       
Out[9]: '         welcome to this world          '


In [10]: string.center(40,'-')    ##.调用方式,括号内放置方式的参数                                                
Out[10]: '---------welcome to this world----------'

【注】此处的所有类型都可作为内置函数,进行转化

##类型作为内置函数

##定义类型

In [12]: a = 1                                                                  

In [13]: b = 2.3                                                                

 ##直接类型转化                    

In [16]: int (a)                                                                
Out[16]: 1

In [17]: int (b)                                                                
Out[17]: 2

In [18]: float (a)                                                              
Out[18]: 1.0

In [20]: str (b)     ##数字和字符之间的转化,但字符转化不到数字                                                           
Out[20]: '2.3'

3、逻辑运算符

##逻辑运算符
#与 and:表示and前后均成立条件为真(一假为假)
condition1(true) and condition2(true) = ture
condition1(true) and condition2(false) = false

#或 or:表示or条件前后有一个为真即为真(一真为真)
condition1(false) or condition2(true) = ture
condition1(false) or condition2(false) = false

#非 not:表示对当前条件取反
not condition(false) = true
not condition(true) = false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值