Python--初识

  • Python 是一种解释性,面向对象的程序设计语言。
  • Python 严格区分大小写
  • 程序设计错误可以分为三种类型:

    1.语法错误
    2.运行时错误
    3.逻辑错误
    
在终端运行Python
在终端输入 Python

运行Python 源文件


python filename.py

简单的打印语句

“`
print(“Python”)
print(5 + 3)

“`

运行Python源文件命令
Python 文件名.py

终端输出

注释

行注释

#This program displays !Welcome to Python

段注释

三个连续单引号
'''This program displays !Welcome to Python
'''
简单的算术
print(1 + 2)
print(8 - 3)
print(2 * 3)
print(6 / 2)
图形化程序设计– Turtle

1.导入Turtle 命令

import turtle # Import turtle module

2.显示Turtle当前位置和方向

turtle.showturtle()

3.绘制文本

turtle.write(" Welcome to python ")

4.绘制一条直线(移动100像素)

turtle.forward(100)

5.箭头旋转角度Turtle颜色改变(自行多尝试)

turtle.right(90)
turtle.color("red")

6.移动Turtle 图形窗口的中心位置初始坐标为(0,0)

turtle.goto(0,50)

7.抬起和放下笔(控制是否是一条直线)

turtle.penup() # 抬起画笔
turtle.goto(-50,50) # 移动位置
turtle.pendown() # 放下画笔

8.画个简单的圆

turtle.color("red")
turtle.circle(50) # radius 50

9.小程序画奥运五环(自行确定好坐标)

turtle.color("blue")
turtle.penup()
turtle.goto(-110,-25)
turtle.pendown()
turtle.circle(45)


turtle.color("black")
turtle.penup()
turtle.goto(0,-25)
turtle.pendown()
turtle.circle(45)

turtle.color("red")
turtle.penup()
turtle.goto(110,-25)
turtle.pendown()
turtle.circle(45)


turtle.color("yellow")
turtle.penup()
turtle.goto(-55,-75)
turtle.pendown()
turtle.circle(45)



turtle.color("blue")
turtle.penup()
turtle.goto(55,-75)
turtle.pendown()
turtle.circle(45)

turtle.done()

效果图

turtle.done() 作用是导致程序暂停知道用户关闭Python Turtle 图形化窗口,它的目的是给用户时间来查看徒刑,没有这行,图形窗口会在程序完成是立即关闭。

### Python 基础语法概述 Python 是一种解释型高级编程语言,以其简洁清晰的语法著称。对于初学者来说,理解其基本结构语法规则是非常重要的。 #### 代码组织与缩进 Python 使用缩进来表示代码块之间的关系,而不是像其他一些编程语言那样使用大括号或其他分隔符。每一级缩进通常由四个空格组成[^2]。例如: ```python def my_function(): print("This is inside the function") # 这里有一个缩进级别 print("This line is outside of the function") ``` #### 控制流语句 Python 提供了几种常见的控制流工具来管理程序执行路径,包括条件判断 `if` 循环 `for`, `while` 等。下面是一个简单的例子展示了如何使用这些语句: ```python number = 10 if number > 5: print(f"{number} is greater than five.") else: print(f"{number} is not greater than five.") for i in range(3): print(i) counter = 0 while counter < 3: print(counter) counter += 1 ``` #### 注释 为了提高代码可读性维护性,在编写过程中添加适当注释是非常有益的做法。单行注释可以通过井号(`#`)实现;而多行注释则可以采用三重引号(无论是三个单引号还是双引号都可以)[^4]: ```python # This is a single-line comment. ''' This is a multi-line comment. It spans multiple lines. ''' """ Another way to write multi-line comments, using triple double quotes instead. """ ``` #### 变量定义 在 Python 中声明变量不需要指定数据类型,直接赋值即可创建新变量。支持多种内置的数据类型如整数(int), 浮点数(float),字符串(str)等: ```python integer_example = 42 # Integer variable floating_point_example = 3.14 # Float variable string_example = "Hello, world!" # String variable ``` 通过上述内容的学习,能够帮助建立起对 Python 编程的基础认识,并为进一步深入学习打下良好基础。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值