day1_python入门

第一个程序

print("hello world")

注:若要打印变量,()里直接写变量名,不加”“

python变量

变量名

1.变量名只能是 字母、数字或下划线的任意组合;
2.变量名的第一个字符不能是数字;
3.以下关键字不能声明为变量名(‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’),即系统已经定义和使用了的特殊关键字。
注:变量名尽量用英文单词,如old_boy。不能使用汉字或者拼音。

变量的赋值
name = "liyanan"
name2 = "lishimi"
print("my name is :", name, name2)
name = "liyifeng"
print(name, name2)

改输出结果是

my name is : liyanan lishimi
liyifeng lishimi

注:第二个输出的name2是lishimi的原因是,name2已经指向”lishimi”,没有随着name的更新而变化。

字符编码

字符编码的历史不做介绍,在python中,python2的字符编码类型是ASCII,python3的字符编码是utf-8,所以,如果定义一个变量内容有中文,python2就会出现编码报错。解决的方法是告诉解释器使用utf-8。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:liyanan

print("你好 世界")

用户输入输出

使用input

name = input("What is your name?")
print("Hello " + name )

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法

import getpass

# 将用户输入的内容赋值给 name 变量
pwd = getpass.getpass("请输入密码:")

# 打印输入的内容
print(pwd)

注:三个引号可以隐藏”’ ** ”’引号里整段话,或者把这段话赋值给一个变量。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:liyanan

name = input("name:")
age = int(input("age:"))
job = input("job:")
salary = input("salary:")


"""info = '''
----------info ''' + name + '''------------
name:''' + age + '''

'''
"""

info2 = '''
--------------info of %s-----------  
name:%s
age:%d
job:%s
salary:%s
''' % (name, name, age, job, salary)


info3='''
--------------info of {_name}---------
name:{_name}
age:{_age}
job:{_job}
salary:{_salary}
'''.format(_name=name,
          _age=age,
          _job=job,
          _salary=salary
)


print(type(age))
print(info3)

%表示占位符,再把变量挨个写出来。%d是整型字符,%s是字符串型,因为默认的字符类型是字符串,所以要用int转换为整型。

if判断

if age == old_of_age:
        print("ok")

if 后跟条件然后冒号回车,python会自动缩进,没有shell中的fi结束,或者java中的done结束,这正是python简明的一点。双层判断用elif,和shell一样。

循环

while循环

格式

count = 0
while True:
    print("count:", count)
    count += 1

例子

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:liyanan

old_of_age = 56


count = 0
while count < 3:
    age = int(input("age:"))
    if age == old_of_age:
        print("ok")
        break
    elif age > old_of_age:
        print("bigger")
    else:
        print("small")
    count += 1
else:
    print("fuck off")
for循环

格式

for i in range(10):
    print("loop", i)

间断输出的格式

for i in range(0,10,2):
    print("loop",i)

例子

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:liyanan


old_of_age = 56


for i in range(3):
    age = int(input("age:"))
    if age == old_of_age:
        print("ok")
    elif age > old_of_age:
        print("bigger")
    else:
        print("small")
else:
    print("fuck off")

continue的作用是结束当前循环,结束本次循环后,再开始下一次的循环,break是结束整个循环。
continue:

for i in range(0, 10):
    if i < 3:
        print("loop", i)
    else:
        continue
    print("hehe")

输出结果:

loop 0
hehe
loop 1
hehe
loop 2
hehe

break:

for i in range(10):
    print('-----------', i)
    for j in range(10):
        print(j)
        if j == 5:
            break

输出结果:

----------- 0
0
1
2
3
4
5
----------- 1
0
1
2
3
4
5
----------- 2
0
1
2
3
4
5
----------- 3
0
1
2
3
4
5
----------- 4
0
1
2
3
4
5
----------- 5
0
1
2
3
4
5
----------- 6
0
1
2
3
4
5
----------- 7
0
1
2
3
4
5
----------- 8
0
1
2
3
4
5
----------- 9
0
1
2
3
4
5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值