python 初学者入门基础

时间:2022-03-24 06:03:24

最近也是不学python不行了呀,然后我就是个小菜鸟,初学python,自己做个小笔记

python 初学者入门基础python 初学者入门基础,我用的是python3,pycharm平台。

1. 向世界说你好,哈哈,总要有个仪式感

print("hello world!")

2.小小变量

name = "Kaixintaozi" name2 = name
print("My name is ", name,name2)

name = "JACK CHEN" print(name,name2)

3.输入:

username = input("username:")
password = input("password:")
print (username,password)
将输入的内容与输出连接起来
name = input("name:")
age = int(input("age:"))  #强行转为整型
print(type(age) , type(str(age)))
job = input("job:")
salary = input("salary:")


info = ''' ------ info of %s ----- Name:%s Age:%d Job:%s Salary:%s '''%(name,name,age,job,salary)
print(info)
info2 =''' ------ info of {_name} ----- Name:{_name} Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name,
            _age=age,
            _job=job,
            _salary=salary)
print(info2)

3.输入用户名和密码:

判断你的输入与之前录入信息是否一致

import getpass

_username = "kaixintaozi" _password = "abc123" username = input("username:")
#password = getpass.getpass("password:")
password = input("password:")
if _username==username and _password==password:  print("Welcom user {name} login...".format(name=username))
else:  print("Invalid useername or password!")

#print(username,password)    #PyCharmpassword根本就不好使
#可以在anaconda中使用(用户名密码)

若是设置用户名和密码,可以在anaconda中执行:

程序:

import getpass

username = input("username:")
password = getpass.getpass("password:")
print (username,password)

步骤:

(1)打开Anaconda prompt

(2)输入c:

(3)输入dir     

这时会出来你的python文件的一个路径,一般情况下都是在PyCharmProjects

(4)输入cd PyCharmProjects

(5)输入dir

这时出现下一层路径,查看到我们一开始建立的一个项目f

(6)输入cd f

(7)输入dir

现在就可以看到你的py文件

(8)输入python password2.py

这样就可以在Anaconda 中运行了

4.猜年龄

这个首先是你给定了一个年龄,让别人猜一下看是否正确

age_= 24

guess_age = int(input("guess age:"))

if guess_age == age_ :  print("Congraulations! You are right!")
elif guess_age > age_ :  print("Think smaller...")
else:  print("Think bigger...")

上面只能才一次

下面可以猜3次

age_ = 24

for i in range(3):  guess_age = int(input("guess age:"))
    if guess_age==age_:  print("Congratulations!You are right!")
        break #break是结束当前整个循环
    elif guess_age > age_:  print("Think smaller...")
    else:  print("Think bigger...")
else:  print("You have tried too much times...")
#改进,可以多猜几次
count = 1
while count<= 3:  guess_age = int(input("guess age:"))
    if guess_age==age_:  print("Congratulations!You are right!")
        break  elif guess_age > age_:  print("Think smaller...")
    else:  print("Think bigger...")
    count = count+1
    if count>3:  continue_confirm = input("Do you want keep guessing ? y/n")
        if continue_confirm == "y":  count = 1
        elif continue_confirm == "n":  break