[python]初步练习脚本

之前练习的python,编写的脚本,现在作为记录,方便查看~

python 初步练习脚本

  • 基础部分的练习脚本
    • 脚本代码
        • 1、helloworld.py,有for循环语句
        • 2、main.py
        • 3、range—test.py,范围
        • 4、RE.py,花式输出内容
        • 5、turtle练习.py,画图库练习
        • 6、测试温度.py
        • 7、乘法表.py
        • 8、绘制蛇.py
        • 9、数字形式转换.py
        • 11、数值运算.py
        • 12、温度转换.py

基础部分的练习脚本

脚本代码

1、helloworld.py,有for循环语句

a=int(input("请输入一个整数:"))
b=["H","e","l","l","o"," ,""W","o","r","l","d"]
if a == 0:
    print(",".join(b))
elif a > 0:
    for x in range(0,11):
        if x>0 and x%2==0:
            print()
        print(b[x],end="")
else:
    for y in b:
        print(y)

运行结果:

[python]初步练习脚本_第1张图片

2、main.py

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

运行结果:

[python]初步练习脚本_第2张图片

3、range—test.py,范围

sum=0
for x in range(1,101,3):
    if x%2==0:
        print(x,end=" ")
        sum=sum+x
print(sum)

运行结果:

[python]初步练习脚本_第3张图片

4、RE.py,花式输出内容

# coding=utf-8
import re
x="Hello, happy day"
print(re.search(".",x))
print(re.match("Hello",x))
print(re.split(",",x))
print(re.findall("[Hh]",x))

运行结果:

[python]初步练习脚本_第4张图片

5、turtle练习.py,画图库练习

1、

import turtle
#turtle.goto(100,100)
#turtle.goto(100,-200)
#turtle.goto(0,0)

 
# 练习1

turtle.left(45)
turtle.fd(100)
turtle.right(135)
turtle.fd(200)
turtle.right(45)
turtle.bk(100)

运行结果:

[python]初步练习脚本_第5张图片

2、

import turtle
#turtle.goto(100,100)
turtle.goto(100,-200)
#turtle.goto(0,0)

 
#练习2

turtle.left(45)
turtle.fd(100)
turtle.right(135)
turtle.fd(200)
turtle.right(45)
turtle.bk(100)

运行结果:

[python]初步练习脚本_第6张图片

3、

import turtle
turtle.goto(100,100)
#turtle.goto(100,-200)
#turtle.goto(0,0)

 
#练习3

turtle.left(45)
turtle.fd(100)
turtle.right(135)
turtle.fd(200)
turtle.right(45)
turtle.bk(100)

运行结果:

[python]初步练习脚本_第7张图片

6、测试温度.py

#这是一个测试题
TempSrt=input("请输入带符号的温度值:")
if TempSrt[-1] in ["f","F"]:
    c=(eval(TempSrt[1:-1])-32)/1.8
    print("转换后的温度为:{:.2f}C".format(c))
elif TempSrt[-1] in ["c","C"]:
    f=1.8*eval(TempSrt[0:-1])+32
    print("转换后的温度为:{:.2f}F".format(f))
else:
    print("输入错误!") 

运行结果:

在这里插入图片描述

7、乘法表.py

for i in range(1,10):
    for j in range(1,i+1):
        print(f" {j}*{i}={i*j:0>2}",end="")
    print()

运行结果:

[python]初步练习脚本_第8张图片

8、绘制蛇.py

import turtle
turtle.setup(600,300)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(10)
turtle.pencolor("blue")
turtle.seth(-40)
for i in range(3):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()

运行结果:

[python]初步练习脚本_第9张图片

9、数字形式转换.py

template = "零一二三四五六七八九"
s = input()
for c in s:
    print(template[eval(c)], end="")

运行结果:

[python]初步练习脚本_第10张图片

11、数值运算.py

s=input()
print("{:.2f}".format(eval(s)))

运行结果:

在这里插入图片描述

12、温度转换.py

#变量:程序里用于保存和表示数据的占位符。TempStr、 C、F

TempStr = input("输入带符合的温度值:")
if TempStr[-1] in ['F','f']:
    C=(eval(TempStr[0:-1])-32)/1.8
    print("转换后温度为{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
    F=1.8*eval(TempStr[0:-1])+32
    print("转换后温度为{:.2f}F".format(F))
else:
    print("输入格式有误")

运行结果:

[python]初步练习脚本_第11张图片

你可能感兴趣的:(Python,python,pycharm,开发语言)