pytnon 学习day-1

Python入门:变量定义与命名规范
博客主要针对Python初学者,介绍了第一个代码相关内容,重点讲解了变量的概念,包括变量、变量值和变量名。还阐述了Python语言的命名规范,如以英文字母或下划线开头等,同时指出定义变量名不好的方式,以及变量的三大特征。

初学python

 

第一个代码

print('hello world!')

 

什么是变量?

变量:可变化的量

变量值:会在内存中产生一份内存地址

变量名:用于变量的绑定

name='tank' 
print(name)  #tank

 

python语言的命名规范:

1、要以英文字母或者下划线开头命名

2、不能以数字开头命名

3、关键字不能命名

 

定义变量名不好的方式

1、不要以中文命名

2、变量名过长

3、变量名词不达意

 

定义变量的三大特征

id #用来表示变量的值在内存中唯一的内存地址
type #变量值的类型
value #变量的值

 

用户与程序交互
    输入:
        input()
    输出:
        print()
 
格式化输出
    尊敬的用户,你好!您本月的话费扣除99元,还剩0元。
    # 通过某种占位符,用于替换字符串中某个位置的字符。
    占位符:
        %s: 可以替换任意类型
        %d: 可以替换数字类型
    示例:
        尊敬的用户,你好!您本月的话费扣除%s元,还剩%d元。
 
基本数据类型
    1.数字类型:
        整型: int
        浮点型: float
    2.字符串类型
        作用:名字,性别,国籍,地址等描述信息
        定义:在单引号\双引号\三引号内,由一串字符组成
            name='tank'
        优先掌握的操作:
            1、按索引取值(正向取+反向取) :只能取
            2、切片(顾头不顾尾,步长)
            3、长度len
            4、成员运算in和not in
            5、移除空白strip
            6、切分split
            7、循环
# 按索引取值
#正向取
str1 = 'hello world!'
print(str1[0])
print(str1[9])

#反向取
print(str1[-2])

#切片(顾头不顾尾)
print(str1[0:5])

#步长
print(str1[0:11])
print(str1[0:11:2])

#长度len
print(len(str1))

#成员运算in和not in
print('h' in str1)
print('h' not in str1)

#移除空白strip
#会移除字符串中左右两边的空格
str1 = '   hello world!'
print(str1)
str1 = '   hello world   '
print(str1)
print(str1.strip())

#去除指定字符串
str2 = '!world'
print(str2.strip('!'))

#切分split
str1 = 'hello world!'
#根据str1内的空格进行切分
#切分出来的值会存放在[]列表中
print(str1.split(' '))


#循环
#对str1字符串进行遍历,打印每一个字符
for line in str1:
    print(line)

# 循环
       
需要掌握的:
            1、strip,lstrip,rstrip
            2、lower,upper
            3、startswith,endswith
            4、format的三种玩法
            5、split,rsplit
            6、join
            7、replace
            8、isdigit
# 1、strip,lstrip,rstrip
# str1 = '  hello wuyuefeng   '
# print(str1)
# # 去掉两边空格
# print(str1.strip())
# # 去掉左边空格
# print(str1.lstrip())
# # 去掉右边空格
# print(str1.rstrip())


# 2、lower,upper
# str1 = 'hello WuYueFeng'
# # 转换成小写
# print(str1.lower())
# # 转换成大写
# print(str1.upper())

# 3、startswith,endswith
# str1 = 'hello WuYueFeng'
# # # 判断str1字符开头是否等于hello
# print(str1.startswith('hello'))  # True
# # # 判断str1字符末尾是否等于WuYueFeng
# print(str1.endswith('WuYueFeng'))  # True
#
# # 4、format(格式化输出)的三种玩法
# # str1 = 'my name is %s, my age %s!' % ('tank', 18)
# # print(str1)
#
# # 方式一:  根据位置顺序格式化
# print('my name is {}, my age {}!'.format('tank', 18))
#
# # 方式二:  根据索引格式化
# print('my name is {0}, my age {1}!'.format('tank', 18))
#
# # 方式三:  指名道姓地格式化
# print('my name is {name}, my age {age}!'.format(age=18, name='tank'))

# 5、split 切分


# 6、join 字符串拼接
# 报错,只允许字符串拼接
# print(' '.join(['tank', 18]))
# # 根据空格,把列表中的每一个字符串进行拼接
# print(' '.join(['tank', '18', 'from GZ']))
# # 根据_,把列表中的每一个字符串进行拼接
# print('_'.join(['tank', '18', 'from GZ']))

# 7、replace:字符串替换
# str1 = 'my name is WangWei, my age 73!'
# print(str1)
# str2 = str1.replace('WangWei', 'sb')
# print(str2)

# 8、isdigit:判断字符串是否是数字
choice = input('请选择功能[0, 1, 2]: ')
# 判断用户输入的选择是否是数字
print(choice.isdigit())

 

 

   

 

 

 

 

转载于:https://www.cnblogs.com/talk-one/p/11076990.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值