Python--01

这篇博客介绍了Python的基础,包括基本数据类型如字符串和数字,以及如何进行字符串的加法和乘法操作。此外,还详细讲解了条件语句`if`的用法,如基本结构、嵌套条件和多条件判断。最后,讨论了`while`循环语句,提到了`continue`和`break`在循环控制中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

目录

Python基础

基本数据类型

if 条件语句

While 循环语句


文件名 .py

执行方式:1、文件路径           2、进入解释器中

文件头:确保在python2&3中顺利执行

第一行:Linx系统下说明解释器位置

第二行:使用utf-8编码方式--中文编码方式,需3个字节,python2中使用

     注:GBK中文编码方式,需2个字节

#!/user/bin/u/ubv/a python
# -*- coding:utf-8 -*-

 

变量组成:字母、数字、下划线

  • 不以数字开始
  • python关键字不能使用
  • python内置内容不能使用

单行注释  #

多行注释   首末行使用三个双引号

基本数据类型

字符串:可用单引号、双引号、三单引号、三双引号

  • 加法
n1='1'
n2='2'
n3=n2+n1
n3='12'
  • 乘法
n1='1'
n2=n1*5
n2='11111'

数字:age=13

 

字符串转数字:new_inp=int(inp)

  • 加减乘除
  • //       除法取整
  • %     除法quyu取余
  • **      幂运算

if 条件语句

if基本

if n>3:
    print('n is greater than 3')
else: 
    print('n is not greater than 3') 

 

 if 镶嵌

if n>3:
    print("n is greater than 3")
    if n=3:
        print("n equals 3")
    else:
        pass
else: 
    print("n is smaller than 3")

if多条件

if n>3 and n<5:
    print(n)
else:
    pass
n=3
if n==1
    print("BOSS")
elif n==2
    print("CEO")
elif n==3
    print("TEACHER")
else:
    print(""STU)

While 循环语句

基础循环语句

n=5
while n<16:
    n=n+1

print(n)

 

n=5
while n<7:
    n=n+1
    print("n<7")
else:
    print("n=7")

    

continue 结束当前循环,开始下一次循环

n=0
while n<10:
    if n==7:
        n=n+1
        continue
    print(n)
    n=n+1

 

break 结束所有循环

n=0
while n<10:
    if n==7:
        n=n+1
        break
    print(n)
    n=n+1
n=0
i='1'
j='2'
while n<3:
    user=input("please input your user id")
    pwd=input("please input your password")
    if user==i and pwd==j:
        print("welcome!")
        break
    else:
        print("your user id or password are wrong, please input again")
    n=n+1

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值