1.Python学习笔记:[Print()格式;缩进;变量命名;注释;字符串拼接;表达式和运算符;变量赋值;输入;强制类型转换;IF语句]...

一、Print()格式:

print()   #等价于print(end="\n")
print('hello word')    或    print("hello word")

print('hello word\n'*8)
结果:
 
 
print('hello word')
print('hello word')
print('hello word')
结果:
print('hello word',end=" ")    #输出在同一行
print('hello word',end=" ")
print('hello word',end=" ")

结果:

 二、缩进:

  1. Tab不等于四个空格
  2. 缩进级别一致(官方建议四个空格)

三、变量命名:

  1. 具有描述性:Student_number(良好习惯)
  2. 支持中文命名,但不推荐
  3. 不能以数字开头
  4. 变量名只能数字字母组成,不可一是空格或特殊字符(#?<,./$%^#等)
  5. 保留字符不用做变量:print=5(不可)
  6. Python不分常量,变量。全部大写默认常量(STUDENT_NUMBER)
  7. Python区分大小写

四、注释:

单行注释:

#print()

多行注释:(三引号。单引,或双引)

'''print()
print()
print()'''

 解释:

print()     #打印

多行打印:

msg='''hello word
hello word
hello word'''
print(msg)
结果:

 五、字符串类型(string):

 字符串拼接:

print("good"+"well")

结果: 

a='123'
b='abc'
c='789'
d2='---'.join([a,b,c])
print(d2) #123---abc---789 用---拼接(建议用这拼接)
print('helloworld'[2:]) #lloworld  这里和列表的切片操作是相同的
print('el' in 'hello')  #True   成员运算符 - 如果字符串中包含给定的字符返回 True

六、表达式和运算符:

算术运算符 : +   -    *   /     //(取整除,3//2=1)    %(取余)   **(次方)

赋值运算符: = 、+= -= *= /= %= //= **=

>>> num = 2    
>>> num += 1   # 等价于 num = num + 1
>>> num -= 1   # 等价于 num = num - 1
>>> num *= 1   # 等价于 num = num * 1
>>> num /= 1   # 等价于 num = num / 1
>>> num //= 1   # 等价于 num = num // 1
>>> num %= 1   # 等价于 num = num % 1
>>> num **= 2   # 等价于 num = num ** 2

比较运算符:>、 <、 >=、 <=、 ==、!=

逻辑运算符: not 、and、 or

>>> a > b and  a < b  # 如果两个操作数都是True,那么结果为True,否则结果为False。
False
>>> a > b or  a < b  # 如果有两个操作数至少有一个为True, 那么条件变为True,否则为False。
True
>>> not a > b  # 反转操作的状态,操作数为True,则结果为False,反之则为True
False

成员运算符: not in 、in (判断某个单词里是不是有某个字母)

>>> "h" in "hello"  # 这里的意思是 “h” 在“Hello” 中,判断后结果为True
True 
>>> "h" not in "hello"  # 这里的意思是 “h” 不在“Hello” 中,判断后结果为False
False

身份运算符: is、is not(讲数据类型时讲解,一般用来判断变量的数据类型)

>>> a=123
>>> b=123
>>> a is b
True
>>> a is not b
False
print('3*4=',3*4)

x=3
y=4
print('x*y=',x*y)

x=3
y=4
z=x*y
print(z)

优先级用():     >>> (((2+3)*2+3)/2)*5

七、变量赋值:

name="梁志伟"
name2="小李"
print(name,name2)

结果: 

八、输入:

age=input('你的年龄:')  #默认输入的是字符串
change=int(age)

九、强制类型转换

>>> a=3
>>> int(a)
3
>>> str(a)
'3'
>>> float(3)
3.0

 十、IF语句:

if (3>2) and (4>2) :   #and  or
if a<b<c :   #在python可以
if 1<=z<=5 :
    print('')
else :
    print('')

if score>90 :
    print('优秀')
elif score>80 :
    print('良好')
elif score>60 :
    print('及格')
else :
    print('')

#score=91   结果为:优秀

转载于:https://www.cnblogs.com/LiangZhiWei/p/9185083.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值