Python 3基础

Python 3基础

Python是Guido van Rossum在90年代早期开发的,它的最新版本是3.7.1,我们可以简单地称之为Python3。Python3.0于2008年发布。是一种解释性语言,即它没有编译,解释器将逐行检查代码。本文可以用来学习Python编程语言的基本知识。

所以在继续之前..让我们来做最流行的“HeloWord”的传统,并将Python的语法与C、C++、java进行比较。

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

print("Hello World") /<code>

请注意,Python的范围不取决于括号{},而是使用缩进作为范围。

现在继续前进,让我们开始了解Python的基础知识。我将在一些小节中介绍基础知识。只要仔细阅读它们,并相信我,您会很容易地学习Python的基础知识。

简介和设置

1)如果您使用的是Windows操作系统,请点击此处下载Python(https://www.python.org/downloads/windows/),然后从安装程序中安装,并在开始菜单中输入IDLE.IDLE,您可以将其视为运行Python脚本的Python IDE。

看起来会是这样:

Python 3基础

2)99%的Linux操作系统上预安装Python。如果您使用的是类似Linux /Unix的操作系统,请打开终端,然后在终端中键入“python3”即可开始使用。

它看起来像这样:

Python 3基础

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

变量和数据结构

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

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

myNumber2 = 4.5
print(myNumber2)

myNumber ="helloworld"
print(myNumber) /<code>

输出:

<code>3
4.5
helloworld/<code>

看,它有多简单,只需创建一个变量并为其分配所需的任何值,然后使用打印功能即可将其打印出来。Python有4种内置的数据结构类型,即列表(List),字典(Dictionary),元组(Tuple)和集合(Set)。

List是python中最基本的数据结构。列表是可变的数据结构,即在创建列表之后,可以将项添加到列表中。就好像你要去当地市场购物,列出一些商品的清单,然后你可以在清单上添加越来越多的商品。

append() 函数用于将数据添加到列表中。

<code># Python program to illustrate list  

# creates a empty list
nums = []

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

print(nums) /<code>

输出:

<code>[21, 40.5, String]/<code>

注释:

<code># is used for single line comment in Python
""" this is a comment """ is used for multi line comments/<code>

输入输出

在本节中,我们将学习如何从用户那里获取输入,然后操纵或简单地显示它。 input()函数用于接收用户的输入。

<code># Python program to illustrate 
# getting input from user
name = input("Enter your name: ")

# user entered the name 'prog61'
print("hello", name) /<code>

输出:

<code>hello prog61/<code>
<code># Python3 program to get input from user 

# accepting integer from the user
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))

num3 = num1 * num2
print("Product is: ", num3)
/<code>

输出:

<code>Enter num1: 8 Enter num2: 6 ('Product is: ', 48)/<code>

选择(Selection)

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

<code># 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") /<code>

输出:

<code>Num1 is good/<code>

函数

您可以将函数想像成一堆旨在在整个Python脚本中完成特定任务的代码。Python使用关键字“ def”来定义函数。

语法:

<code>def function-name(arguments):
#function body/<code>
<code># Python program to illustrate 
# functions
def hello():

print("hello")
print("hello again")
hello()

# calling function
hello() /<code>

输出:

<code>hello
hello again
hello
hello again/<code>

现在我们知道任何程序都从“main”函数开始……让我们像许多其他编程语言一样创建main函数。

<code># 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() /<code>

输出:

<code>Started
Enter integer: 5/<code>

迭代(循环)

顾名思义,它要求一次又一次地重复。我们将在此处使用最流行的“for”循环。

<code># Python program to illustrate 
# a simple for loop

for step in range(5):
print(step) /<code>

输出:

<code>0
1
2
3
4/<code>

模块

Python有一个非常丰富的模块库,其中包含一些函数来执行许多任务。

关键字“import”用于将特定模块导入到您的python代码中。例如,考虑以下程序。

<code># 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() /<code>

输出:

<code> Enter a number: 85.0/<code>

这些是Python编程语言的一些最基础知识,我将在我的后续文章中介绍中级和高级Python主题。


分享到:


相關文章: