刚刚开始自学Python,整理一下自己的学习感悟

   刚刚开始学习Python,代码之路才刚刚开始第一个差距就感受到了。Python的标点符号与其他语言的差别,它每句后面都没有“;”。

变量的命名规则
    1. 要具有描述性
    2. 变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
    3. 不能以中文为变量名
    4. 不能以数字开头
    5. 保留字符是不能被使用

常量 :不变的量 pie = 3.141592653....
    在py里面所有的变量都是可变的 ,所以用全部大写的变量名来代表次变量为常量

注释
    单行注释 用#
    多行注释用三个单引号或三个双引号 '''被注释的内容'''

表达式if ...else语句
    缩进 IndentationError: expected an indented block       ####此错误为没有缩进              ^
    IndentationError: unindent does not match any outer indentation level
    tab != 4个空格,缩进级别必须保持一致 ###假如使用了tab键进行缩进,那么在本次的缩进必须全部使用tab键进行缩进,官方要求使用4个空格。

and  且,并且

     只有两个条件全部为True(正确)的时候, 结果才会为True(正确)

   ex:条件1 and 条件2

    5>3 and 6<2  True

    对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式 的计算结果就一定为假,第二个条件就不会被计算

or 或,或者
     只要有一个条件为True,则结果为Ture,
     ex:5>3 or 6<2  True

     对于or ,如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式 的计算结果就一定为真,第二个条件就不会被计算

not  不 取反

循环loop
    有限循环 ,次数限制
    无限循环=死循环
    continue 结束本次循环,继续下一次循环
    break 跳出整个当前的循环

while循环:当条件为真时执行循环里的语句
      while 条件:
             print("any")
             print("any")

猜年龄的小程序

age = 50
#user_input_age = int(input("Age is :"))
#flag = True
# break  
while True:
    user_input_age = int(input("Age is :"))
    if user_input_age == age:
        print("Yes")
        break
    elif user_input_age > age:
        print("Is bigger")
    else:
        print("Is smaller")      
print("End")

for循环小程序

#_author: 五星12138
#date: 2017/8/23
_user = "bcb"
_password = "bai199537"
passed_authtication = False
for i in range(3):
    username = input("username:")
    password = input("password:")
    if username == _user and password == _password:
        print("welcome %s login"% _user)
        passed_authtication = True
        break
    else:
        print("invalid username or password")
else:
    print("登录次数过多")#只要for循环正常结束完毕就会执行else,即没有被break
# if passed_authtication == False:
#     print("登录次数过多")
数据运算
    数据类型出初识
        数字
            整数  int(integer)
                整型
                长整型
                in py3 已经不区分整型与长整型,统一都叫整型
                in C int age 22 , long age
        布尔 只有2种状态,分别是
            真 True
            假 False
        字符串
            salary.isdigit()
            计算机中, 一切皆为对象
            世界万物,皆为对象,一切对象皆可分类

字符格式化输出:通过占位符
 占位符 %s  s = string
       %d  d = digit 整数
       %f  f = float 浮点数,约等于小数

格式化输出小程序:

#_author: 五星12138
#date: 2017/8/23
name = input("name:")
age = input("age:")
sex = input("sex:")
salary = input("salary:")
if salary.isdigit() :
    salary = int(salary)
# else :
#     exit("must be digt")
msg = """------info of %s
name:%s
age:%s
sex:%s
salary:%d
"""%(name,name,age,sex,salary)
print(msg)