100个必会的python脚本,python脚本入门教程

本篇文章给大家谈谈100个必会的python脚本,以及python脚本入门教程,希望对各位有所帮助,不要忘了收藏本站喔。

目录

前言

01-hello world!

02-print.py

03-基本运算.py

04-input.py

05-字符串的使用

06-列表基础

07-元组基础

08-字典基础

09-基本判断语句

10-三元运行符

前言

脚本运行在CentOS 7环境下的,请知晓!!!学习一门语言,最好的方法就是多敲!!!

  • 01-hello world!

#!/usr/local/bin/python3   #解释器的环境变量

print('hello world')   
print("hello world")       #python中的单双引号没区别

if 3 < 0:                  #判断语句时,要加分号:
    print('ok')
    print('123')   #判断语句下的语句缩进表示以属于if 3<0判断语句的,3<0不生效,包含的语句不执行
print('no')        #正常输出,此语句不缩进,表示不属于if 3<0判断语句的       

if 3 > 0:
    print('Today is Tuesday!')  #3>0条件成功,此语句正常输出

x = 3 ; y = 4    #不推荐,建议写成两行
print(x + y)     #输出x+y的和

a = 8
b = 8
print(a+b)

01-hello world!运行的结果为:

hello world
no
Today is Tuesday!
7
16

 

  • 02-print

#!/usr/local/bin/python3
print('--字符串空格--')
print('hello world!')      #字符串也包含“ ”空格
print('hello','world!')    #逗号用于空格字符串间的

print()   #表示空一行
print('--字符串拼接--')
print('hello''world!')     #字符串间默认拼接
print('hello'+'world!')    #加号用于字符串间拼接

print()
print('--print中\\n换行--')  #用\n前用\表示转义
print('abc')
print('a\nb\nc')

print('----------')    #表示分隔符
print('--print中*的用法--')
print('3' * 5)   #print中字符用*号表示字符重复多少次
print(3 * 5)     #print中数字用*号表示求积算法

print('----------')
print('--print中end的用法--')
print('Nothing is impossible to a willing heart!')            #字符默认是换行的
print('Nothing is impossible to a willing heart!', end='')    #end=''表示不换行,单引号内没有空格的
print('Nothing is impossible to a willing heart!', end='\n')  #end='\n'和\n都是换行

print('----------')
print('--print中的sep表示两单词间用什么字符分隔--')
print('hello','world', sep='***')
print("file\n","abc","fff",sep='###\n')   #换行间的字符串间也会被“###”分隔

02-print.py运行结果为:

  • 03-基本运算

#!/usr/local/bin/python3
#优先级是:算术运算符>比较运算符>逻辑运算符
print('--算术运算符-------------')
print(5 / 2)  #2.5  除法

print(5 // 2)  #2
print(-5 // 2) #-3  print中的//号是向下取正

print(5 % 2)   #3  求余
print(5 * 2)   #10 乘法
print(5 ** 2)  #25 求5的平方
print(5 ** 3)  #125  求5的立方
print()
print('--比较运算符-------------')
print(5 > 3)   #返回true结果
print(5 < 3)   #返回false结果
print(8 == 8)  #true
print(5 >= 3)  #true
print(8 != 8)  #false

print(20 > 10 > 5)  #true  python支持连续比较
print()
print('--逻辑运算符"and、or和not"-------------')
print(20 > 10 and 10 > 5)  #与print(20 > 10 > 5)相同含义
print(not 20 > 10)   #false

print()
print('--赋值运算符"=、+=、-=、/=、%=、**=、//="--')
a = 1
b = 2
a += 1
b **= 2
print(a)   #2
print(b)   #4
  • 04-input

#!/usr/local/bin/python3
number = input('please input the number: ')   # input用于获取键盘输入
print(number)
print(type(number))   # input获得的数据默认是字符型

#print(number + 10)       # 报错,不能把字符和数字做运算(后注释掉)
print(int(number) + 10)  # int可将字符串10转换成数字10
print(number + str(10))  # str将10转换为字符串后实现字符串拼接


#把input的str类型更改为int类型
#int(shuzi) = input('输入:')
#print(shuzi + 10)    #这两步为错误的,正确如下
shuzi = int(input('shuzi=: '))
print(shuzi + 10)

04-input.py运行的结果为:

  • 05-字符串的使用

#!/usr/local/bin/python3
#python中单双引号表示字符串是没有区别的
print('Today is Wednesday!')
print("Today is Wednesday!")

#转义是在需要转义的字符前面加\
print('tom\'s pet is a cat')
print("tom's pet is a cat")
print("tom said:\"hello world!\"")
print('tom said:"hello world!"')

#三个连续的单、双引号之间,允许输入多行字符串
a = '''
Today
is
Wednesday!'''

b = """
You
are
beautiful"""

print(a)
print(b)

#截取字符串
py_str = 'python3'
print(len(py_str))   #7 取字符的长度
print(py_str[0])     #p 取第一个字符
print(py_str[-1])    #3 取最后一个字符
#print(py_str[7])    #报错,下标超出范围。起始上标由0开始,python3的下标由0到6
print(py_str[2:4])   #th 切片,起始下标包含,但不包含结束下标
print(py_str[2:])    #thon3 从下标为2的字符取到结尾
print(py_str[:2])    #py 从开头取到下标是2之前的字符
print(py_str[:])     #python3 取全部

#取步长
print(py_str[::2])   #pto3 步长值为2,默认是0
print(py_str[1::2])  #yhn
print(py_str[::-1])  #3nohtyp 步长为负,自右向左取,实现逆写

print(py_str + ' is good')  #注意is左边是有个空格字符
print(py_str * 3)    #字符重复三次
print('t' in py_str) #trye
print('th' in py_str) #trye
print('th' not in py_str) #false

05-字符串的使用运行结果为:

  • 06-列表基础

   列表也是序列对象,但它是容器类型,列表中可以包含各种数据python可以自学吗需要什么基础

#!/usr/local/bin/python3
list = [10, 20 ,30, 'abc', '123', [1,2,3] ]  #定义一个列表
print(type(list))  #<class 'list'> 显示列表类型
print(len(list))   #6
print(list[-1])    #[1,2,3] 取出最后一项
print(list[-1][-1])   #3 最后一项是列表,列表还可以继续取下标
print(list[-2][-2])   #2
print(list[3])     # abc
print(list[3:5])   #['abc', '123']
print(100 in list)  #false

list[-1] = 100     #修改最后一项的值
list.append(888)   #向列表中追加一项
print(list)        #[10, 20, 30, 'abc', '123', 100, 888]
  • 07-元组基础

  元组与列表基本上是一样的,只是元组不可变,列表可变

#!/usr/local/bin/python3
tuple = (10, 20, 30, 'abc', '123', [1,2,3])
print(len(tuple))    # 6
print(10 in tuple)   # true
print(tuple[2])      # 30
print(tuple[3])      # abc
print(tuple[3:5])    # ('abc', '123')
#tuple[-1] = 100  #报错,元组是不可变的
  • 08-字典基础

字典是key-value(键--值)对形式的,没有顺序,通过键取出值。

#!/usr/local/bin/python3
dictionary = {'name':'python3', 'age':18}
print(dictionary)
print(len(dictionary))

print(dictionary['name'])       #python3 输出name的键值
print('python3' in dictionary)  #false
print('name' in dictionary)     #true

dictionary['email'] = 'python3@qq.com'  # 字典中没有key,则添加新项目
dictionary['age'] = 30   #字典中已有key,修改对应的value

print(dictionary)

08-字典基础的使用运行结果为:

{'name': 'python3', 'age': 18}
2
python3
False
True
{'name': 'python3', 'age': 30, 'email': 'python3@qq.com'}

  • 09-基本判断语句

单个的数据也可作为判断条件。
任何值为0的数字、空对象都是False,任何非0数字、非空对象都是True

#!/usr/local/bin/python3
if 3 > 0:
    print('yes')
    print('one')

if 10 in [10, 20, 30]:
    print('ok')
    print('two')

if -0.0:
    print('yes')   # 任何值为0的数字都是False
    print('three')

if [1, 2]:
    print('yes')   # 非空对象都是True
    print('four')

if ' ':
    print('yes')   # 空格字符也是字符,条件为True
    print('five')


09-基本判断语句运行的结果为:

yes
one
ok
two
yes
four
yes
five

  • 10-三元运算符

#!/usr/local/bin/python3
a = 10
b = 20

if  a > b :
     max = a 
else :
     max = b
print(max)

max = a if a > b else b  #三元运算符,与上面的if-else判断作用一样
print(max)


++++结果++++
20
20

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值