Python 3基础
Python由Guido van Rossum于1990年代初期开发,其最新版本为3.7.1,我们可以简单地将其称为Python3。Python 3.0于2008年发布。它是一种解释语言,即未编译,解释器将逐行检查代码。本文可以用来学习Python编程语言的基础知识。
因此,在继续之前..让我们做最流行的'HelloWorld'传统,??然后将Python的语法与C,C ++,Java进行比较(之所以选择这三种,是因为它们是最著名和最常用的语言)。
filter_none
编辑
play_arrow
亮度_4
# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.
print("Hello World")
注意:请注意,Python的范围不取决于括号({}),而是使用缩进作为其范围。
现在继续前进,让我们开始了解Python的基础知识。我将在一些小节中介绍基础知识。只需遍历它们并相信我,您将非常轻松地学习Python的基础知识。
简介和设置
如果您使用的是Windows操作系统,请单击此处下载Python,然后从安装程序中安装,并在开始菜单中输入IDLE.IDLE,您可以将其视为运行Python脚本的Python IDE。
看起来会这样:
如果您使用的是类似Linux / Unix的操作系统,请打开终端并在99%的Linux操作系统上预安装Python,只需在终端中键入``python3''即可开始使用。
它看起来像这样:
“ >>>”表示python shell及其准备好接受python命令和代码的地方。
变量和数据结构
在其他编程语言(例如C,C ++和Java)中,您需要声明变量的类型,但是在Python中,您不需要这样做。只需键入变量,并在将其赋值时,它将自动知道给定的值是int,float还是char甚至是String。
filter_none
编辑
play_arrow
亮度_4
# Python program to declare variables
myNumber = 3
print(myNumber)
myNumber2 = 4.5
print(myNumber2)
myNumber ="helloworld"
print(myNumber)
输出:
3
4.5
你好,世界
看,它有多简单,只需创建一个变量并为其分配所需的任何值,然后使用打印功能即可将其打印出来。Python有4种内置的数据结构类型,分别是List,Dictionary,Tuple和Set。
List是python中最基本的数据结构。列表是可变的数据结构,即可以在创建列表之后稍后将项目添加到列表。就像您要在本地市场上购物并列出了一些商品的清单一样,以后您可以在清单中添加越来越多的商品。
append()函数用于将数据添加到列表中。
filter_none
编辑
play_arrow
亮度_4
# 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,字符串]
评论:
#用于Python中的单行注释
“”“这是一个注释”“”用于多行注释
输入输出
在本节中,我们将学习如何从用户那里获取输入,然后操纵或简单地显示它。input()函数用于接收用户的输入。
filter_none
编辑
play_arrow
亮度_4
# Python program to illustrate
# getting input from user
name = input("Enter your name: ")
# user entered the name 'harssh'
print("hello"
输出:
你好哈什
filter_none
编辑
play_arrow
亮度_4
# 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: "
输出:
输入num1:8输入num2:6('Product is:',48)
选拔
在Python中的选择是使用两个关键字'if'和'elif'和else(elseif)
filter_none
编辑
play_arrow
亮度_4
# 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好
功能
您可以将函数想像成一堆旨在在整个Python脚本中完成特定任务的代码。Python使用关键字“ def”来定义函数。
句法:
def函数名称(参数):
#功能体
filter_none
编辑
play_arrow
亮度_4
# Python program to illustrate
# functions
def hello():
print("hello")
print("hello again")
hello()
# calling function
hello()
输出:
你好
再一次问好
你好
再一次问好
现在我们知道任何程序都从“ main”函数开始……让我们像许多其他编程语言一样创建main函数。
filter_none
编辑
play_arrow
亮度_4
# Python program to illustrate
# function with main
def getInteger():
result = int(input("Enter integer: "))
return result
def Main():
print("Started")
# calling the getInteger function and
# storing its returned value in the output variable
output = getInteger()
print(output)
# now we are required to tell Python
# for 'Main' function existence
if __name__=="__main__":
Main()
输出:
已开始
输入整数:5
迭代(循环)
顾名思义,它要求一次又一次地重复。我们将在这里使用最流行的“ for”循环。
filter_none
编辑
play_arrow
亮度_4
# Python program to illustrate
# a simple for loop
for step in range(5):
print(step)
输出:
0
1个
2
3
4
模组
Python具有非常丰富的模块库,该库具有用于执行许多任务的多种功能。您可以通过单击此处的
'import'关键字将特定模块导入到您的python代码中来了解有关Python标准库的更多信息。例如,考虑以下程序。
filter_none
编辑
play_arrow
亮度_4
# Python program to illustrate
# math module
import math
def Main():
num = float(input("Enter a number: "))
# fabs is used to get the absolute value of a decimal
num = math.fabs(num)
print(num)
if __name__=="__main__":
Main()
输出:
输入数字:85.0
这些是Python编程语言的一些最基础知识,我将在我的后续文章中介绍中级和高级Python主题。
本文由Harsh Wardhan Chaudhary提供。如果您喜欢GeeksforGeeks并希望做出贡献,则还可以使用contribution.geeksforgeeks.org撰写文章,或将您的文章邮寄到
contribution@geeksforgeeks.org。查看您的文章出现在GeeksforGeeks主页上,并帮助其他Geeks。您的文章将在发布之前先由Geeks for Geeks小组审阅。
如果发现任何不正确的地方,或者想分享有关上述主题的更多信息,请写评论

关注 CDA人工智能学院 ,回复“录播”获取更多人工智能精选直播视频!