【零基础入门Python】Python 3 基础知识

面向读者:所有人

所属专栏:Python零基础入门教程

Python 3 是一种流行的高级编程语言,用于各种应用程序。以下是您应该了解的一些 Python 3 基础知识:

  1. 变量:在 Python 3 中,变量是通过将值分配给名称来创建的。例如,x = 5 创建一个名为 x 的变量,并为其分配值 5。
  2. 数据类型:Python 3 支持多种内置数据类型,包括整数、浮点数、字符串、布尔值、列表、元组和字典。
  3. 运算符:Python 3 支持多种运算符,包括算术运算符(+、-、*、/)、比较运算符(>、<、==、!=)和逻辑运算符(and、or、not)。
  4. 控制流语句:Python 3 支持多种控制流语句,包括 if-else 语句、for 循环和 while 循环。这些语句允许您控制代码中的执行流程。
  5. 函数:在 Python 3 中,函数是使用 def 关键字创建的。例如,def my_function(x): 创建一个名为 my_function 的函数,该函数采用一个名为 x 的参数。

输入和输出:在Python 3中,您可以使用input()函数获取用户输入,并使用print()函数将文本输出到控制台。

模块:Python 3 支持模块,模块是可以在其他 Python 代码中导入和使用的函数和变量的集合。您可以使用 import 关键字导入模块。

Python 3 的优点:

  1. Python 3 语法简单,易于学习和阅读,是初学者的不错选择。
  2. Python 3 是一种高级语言,拥有大型标准库和许多可用的第三方库,使其成为一种多功能语言,可用于各种应用程序。
  3. Python 3 支持多种编程范例,包括面向对象、函数式和过程式编程。
  4. Python 3 是一种解释性语言,这意味着它在运行前不需要编译,从而可以轻松快速地编写和测试代码。
  5. Python 3 对数据分析和科学计算有很好的支持,有 NumPy 和 Pandas 等库。

Python 3 的缺点:

  1. Python 3 可能比 C++ 或 Java 等编译语言慢,这可能是需要高性能的应用程序的一个问题。
  2. Python 3 具有全局解释器锁 (GIL),这会限制其利用多个 CPU 核心的能力。
  3. Python 3 可能不是低级系统编程的最佳选择,因为它无法提供与其他语言相同级别的硬件控制。
  4. Python 3 在某些领域并不像其他语言那样流行,例如用于数据分析的 R 或用于游戏开发的 C++,因此它可能并不总是特定应用程序的最佳选择

Python是解释性语言,即它不被编译,解释器将逐行检查代码。在继续之前……让我们先采用最流行的“HelloWorld”传统,然后将 Python 的语法与 C、C++ 和 Java 进行比较(我选择了这 3 种语言,因为它们是最著名且最常用的语言)。 

# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.

print("Hello World")	

注意:请注意,Python 的作用域不依赖于大括号 ( { } ),而是使用缩进来表示其作用域。让我们从 Python 基础知识开始,我们将在一些小部分中介绍基础知识。只需阅读它们,相信我,您将非常轻松地学习 Python 的基础知识。

简介和设置

1. 如果您在Windows 操作系统上通过单击此处下载 Python ,现在从安装程序中安装并在开始菜单中键入 IDLE.IDLE,您可以将其视为运行 Python 脚本的 Python IDE。它看起来会是这样的:

2. 如果您使用的是类似 Linux/Unix 的操作系统,只需打开终端,99% 的 Linux 操作系统上都预装了 Python。只需在终端中输入“python3”即可开始使用。它看起来像这样: 

“>>>”代表 python shell 及其准备接受 python 命令和代码。

变量和数据结构

在其他编程语言(如 C、C++ 和 Java)中,您需要声明变量的类型,但在 Python 中则不需要这样做。只需输入变量,当给它赋值时,它就会自动知道给定的值是 int、float、char 还是 String。

# Python program to declare variables
myNumber = 3
print(myNumber)

myNumber2 = 4.5
print(myNumber2)

myNumber ="helloworld"
print(myNumber)

 输出

3
4.5
helloworld

看,多么简单,只需创建一个变量并为其分配任何您想要的值,然后使用 print 函数打印它即可。Python 有 4 种内置数据结构,​即List、Dictionary、Tuple和Set。​

列表是Python中最基本的数据结构。列表是一种可变数据结构,即可以在列表创建后稍后将项目添加到列表中。这就像您要在当地市场购物并列出一些商品,然后您可以向列表中添加越来越多的商品。
append() 函数用于将数据添加到列表中。

# Python program to illustrate a list

# creates a empty list
nums = []

# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")

print(nums)

 输出

[21, 40.5, 'string']
# Python program to illustrate a Dictionary

# creates a empty list
Dict = []

# putting integer values
Dict = {1: 'python', 2: 'For', 3: 'python'}

print(Dict)

#Code submitted by Susobhan AKhuli

输出 

{1: 'python', 2: 'for', 3: 'python'}
# Python program to illustrate a tuple
	
# creates a tuple which is immutable
tup = ('python', 'for', 'python')

print(tup)

#Code submitted by Susobhan AKhuli
(“python”、“for”、“python”)
# Python program to illustrate a set

# define a set and its elements
myset = set(["python", "for", "python"])

#as set doesn't have duplicate elements so, 1 geeks will not be printed
print(myset)

#Code submitted by Susobhan Akhuli

输出

{'python','for'}

 评论:

# 在Python中用于单行注释
"""这是一条注释"""用于多行注释

输入输出

在本节中,我们将学习如何获取用户的输入,从而对其进行操作或简单地显示它。input() 函数用于获取用户的输入

# Python program to illustrate
# getting input from user
name = input("Enter your name: ") 
   
# user entered the name 'harssh'
print("hello", name)

输出:

hello harssh
# Python3 program to get input from user
   
# accepting integer from the user
# the return type of input() function is string ,
# so we need to convert the input to integer
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
   
num3 = num1 * num2
print("Product is: ", num3)

输出:

Enter num1: 8 Enter num2: 6 ('Product is: ', 48)

判断

Python 中的选择是使用两个关键字“if”和“elif”(elseif) 以及 else 进行的

# Python program to illustrate
# selection statement
   
num1 = 34
if(num1>12):
   print("Num1 is good")
elif(num1>35):
   print("Num2 is not gooooo....")
else:
   print("Num2 is great")
输出
Num1 is great

猜你喜欢

转载自blog.csdn.net/arthas777/article/details/133465065