python如何学习(三)

最近开始整理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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值