搜索
首页后端开发Python教程掌握 Python 基础知识日

掌握 Python 基础知识日

Dec 04, 2024 pm 06:00 PM

为了简单起见,我将其分为三个部分:

  • 变量声明
  • 接受输入并声明输出
  • 运算符和表达式

1.变量声明

与 c、cpp 和 java 等其他编程语言不同,您不需要显式声明变量的类型。此功能称为动态输入
在 C 语言中,

int a = 6;

这里,变量被声明为整数。

但是在Python中,

a = 6

并且变量a可以重新声明为

a = "hello world"

?️注意:
变量名称区分大小写.,因此 a 和 A 被视为不同的变量。

✨ 声明中的特点和规则

  • 变量名称必须以字母或下划线 (_) 开头
a = 6    #valid
_a = 6   #valid
-a = 6   #invalid
  • “_”变量存储Python交互模式下最后一个表达式的结果。这可以在 Jupiter Notebook 中看到
a = 5
b = 6
a + b     #11
print(_)  #11 
  • 在Python中,一行中可以声明多个变量
a, b, c = 5, 6, 7
  • 使用global关键字修改函数内的全局变量
global x

def print():
  return x   #there will be no error
  • 与 c 和 cpp 不同,Python 没有内置的方法来声明常量。按照约定,全部大写的变量名被视为常量。
PI=3.14

2.接受输入并声明输出

打印()

print() 函数是一个内置的 Python 函数,用于将输出显示到控制台。

a=10000
print("hello world")   #hello world
print("hello", "world")#hello world
print("hello world",a) #hello world 10000

打印函数中,有两个主要参数。

  • sep :它决定打印时如何分隔多个对象。通常预设为“ ”。
  • end :定义输出末尾打印的内容。通常预设为“n”。

Day Mastering the Basics of Python

示例:

print("hello world")
print("hi")    # hello world
               # hi
print("hello", "world", sep="-", end = " ")
print("hi")    # hello-world hi

有关打印的完整文档,请单击此处

Print() 的特点

  • 您可以使用转义序列(例如,n 表示换行符,t 表示制表符,__ 表示反斜杠)在打印输出中包含特殊字符。
print("hello\nworld")   #hello
                        #world
print("hello\tworld")   #hello   world
print("happy\trecking")#happy\trecking
  • print() 通常用于调试和跟踪代码执行,因为它提供了一种输出变量值和程序状态的快速方法。
x = 5
y = 10
print(f"x: {x}, y: {y}") # x: 5, y: 10

输入()

Python 中的 input() 函数用于从控制台获取用户输入。

int a = 6;

?️注意
默认情况下,input() 返回一个字符串,因此如果您需要将输入用作不同类型(例如 int、float),则需要对其进行转换。

a = 6

在即将发布的博客中,我们将深入研究 Python 运算符和条件语句。快乐学习???

以上是掌握 Python 基础知识日的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

说明如何将内存分配给Python中的列表与数组。说明如何将内存分配给Python中的列表与数组。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python数组中指定元素的数据类型?您如何在Python数组中指定元素的数据类型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什么是Numpy,为什么对于Python中的数值计算很重要?什么是Numpy,为什么对于Python中的数值计算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

讨论'连续内存分配”的概念及其对数组的重要性。讨论'连续内存分配”的概念及其对数组的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),