最近开始整理python的资料,博主建立了一个qq群,希望给大家提供一个交流的同平台 78486745 。
一、第一个Python程序–HelloWorld
python的第一个程序也从hello world开始吧:
#!/usr/bin/env python
#! -*- coding:utf-8 -*-
print("Hello world!")
执行结果:
"C:\Program Files\Python37\python.exe" D:/python/Day1/test/HelloWorld.py
Hello world!
Process finished with exit code 0
二、If-else判断
2.1 If-else语法规范
以下是if-else判断的语法结构规范:
if condition1:
command_layer1_1
if condition2:
command_layer2_2
else:
command_layer2_2
else:
command_layer1_2
2.2 示例程序
以下为一个演示两层if-else循环的程序:
#!/usr/bin/env python #顶格编写
#! -*- coding:utf-8 -*-
user_input = input("Please input you username:")
if user_input == "Bob": #注意这里的冒号结尾
passwd_input = input("Please input your password:") #注意从这里开始,第一个if条件为真时需要执行的动作语句均需要左缩进4个空格
if passwd_input == "password": #第一个if下的第二个if,仍然要左缩进4个空格,同时冒号结尾
print("Welcome login,%s!" %user_input) #第二层if条件为真时执行的动作语句,需要在第一层语句基础上再缩进4个空格,因此需要缩进8个空格
else: #第二层if-else中的else,因此需要与第二层if对齐,缩进4个空格
print("Invalid username or