您的位置 首页 编程知识

Python 教程 – 简介

是目前最流行的编程语言之一,尤其是随着人工智能技术的兴起。 python 是一种多用途编程语言,用于开发 we…

是目前最流行的编程语言之一,尤其是随着人工智能技术的兴起。 python 是一种多用途编程语言,用于开发 web 应用程序、后端服务、数据科学和机器学习等许多东西。

设置

这些是使用 python 编码的准备工作:

  1. 下载python然后安装。
  2. 您可以使用任何文本编辑器来编写 python 代码(例如 visual studio code)或专用 ide(例如 )。

编写第一个代码

创建一个扩展名为 .py 的新文件,名为 main.py。然后编写这段代码。

print("hello world!") 
登录后复制

运行此命令来执行python代码。

python main.py 
登录后复制

这是输出。

hello world! 
登录后复制

基于上面的代码,print()函数显示hello world!文字.

变量和数据类型

变量是存储整数、浮点数和字符串(一堆字母数字字符)等值的地方。这是 python 中变量使用的示例。

number = 42 username = "john doe" price = 2.95 
登录后复制

要显示变量的值,请使用 print() 函数。

number = 42 username = "john doe" price = 2.95  # display a value from variable print("this is a number", number) print("price: ", price) # using formatting print(f"hello, my username is {username}") 
登录后复制

这是输出。

this is a number 42 price:  2.95 hello, my username is john doe 
登录后复制

这是python中常用数据类型的列表。

数据类型 价值
整数 非十进制数
漂浮 十进制数
字符串 字母数字字符
布尔值 是真是假

操作员

python中有很多基本的算术运算符。这些运算符可用于执行整数和浮点等数字数据类型的计算。

立即学习“”;

操作员 描述
+ 添加操作
减法运算
* 乘法运算
/ 除法运算
// 楼层划分操作
% 取模运算(除法运算得到余数)
** 执行数字的幂运算

这是 python 中运算符使用的示例。

first = 4 second = 2  addition = first + second subtraction = first - second multiplication = first * second division = first / second mod = first % second square_of_first = first ** 2  print(f'{first} + {second} = {addition}') print(f'{first} - {second} = {subtraction}') print(f'{first} * {second} = {multiplication}') print(f'{first} / {second} = {division}') print(f'{first} % {second} = {mod}') print(f'{first} ** {2} = {square_of_first}') 
登录后复制

输出

4 + 2 = 6 4 - 2 = 2 4 * 2 = 8 4 / 2 = 2.0 4 % 2 = 0 4 ** 2 = 16 
登录后复制

// 运算符执行除法,然后返回除法结果的下限。

result = 29 // 5 # returns 5 (actual value before floor operation: 5.8) 
登录后复制

添加用户输入

input() 函数读取用户的输入。此函数对于在 python 中创建交互式程序非常有用。默认情况下,input() 返回 string 数据类型。

这是使用 input() 函数的基本示例。

# get username from input username = input("enter username: ") # get age from input # the int() function converts string into integer data type age = int(input("enter age: "))  print(f"username: {username}") print(f"age: {age}") 
登录后复制

输出

Python 教程 - 简介

示例 1 – 矩形面积计算

让我们用python 创建一个矩形面积计算程序。该程序允许用户输入矩形的长度和宽度。然后,程序计算矩形的面积,然后将其显示给用户。

# get length from user input length = int(input("enter length: "))  # get width from user input width = int(input("enter width: "))  # calculate the area of rectangle area = length * width  # display the result print(f"area of rectangle: {area}") 
登录后复制

输出

Python 教程 - 简介

示例 2 – 获取折扣价

让我们创建一个程序来计算应用折扣后商品的价格。该程序允许用户输入实际价格和折扣。然后,程序返回折扣价。

# get price from user input price = int(input("enter price: "))  # get discount from user input discount = int(input("enter discount: "))  # calculate the discounted price discounted_price = price - (price * (discount / 100))  # display the result print(f"original price: {price}") print(f"discounted price: {discounted_price}") 
登录后复制

输出

Python 教程 - 简介

来源

  • python 官方页面.
  • python 教程.

希望这篇文章对你学习python有帮助。如果您有任何意见,请在评论区告诉我。

以上就是Python 教程 – 简介的详细内容,更多请关注php中文网其它相关文章!

本文来自网络,不代表四平甲倪网络网站制作专家立场,转载请注明出处:http://www.elephantgpt.cn/1081.html

作者: nijia

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

18844404989

在线咨询: QQ交谈

邮箱: 641522856@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部