龙空技术网

Python 3基础

少儿编程Prog61 137

前言:

现在朋友们对“getpython3”大概比较着重,咱们都想要知道一些“getpython3”的相关内容。那么小编也在网上汇集了一些对于“getpython3””的相关文章,希望看官们能喜欢,小伙伴们快快来学习一下吧!

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

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

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

它看起来像这样:

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

变量和数据结构

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

# Python program to declare variables myNumber = 3print(myNumber)   myNumber2 = 4.5print(myNumber2)   myNumber ="helloworld"print(myNumber) 

输出:

34.5helloworld

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

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

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

# 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) 

输出:

[21, 40.5, String]

注释:

# is used for single line comment in Python""" this is a comment """ is used for multi line comments

输入输出

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

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

输出:

hello prog61
# 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) 

输出:

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

选择(Selection)

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

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

输出:

Num1 is good

函数

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

语法:

def function-name(arguments):            #function body
# Python program to illustrate # functions def hello():     print("hello")     print("hello again") hello()   # calling function hello()                

输出:

hellohello againhellohello again

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

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

输出:

StartedEnter integer: 5

迭代(循环)

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

# Python program to illustrate # a simple for loop   for step in range(5):         print(step) 

输出:

01234

模块

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

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

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

输出:

 Enter a number: 85.0

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

标签: #getpython3