个性化阅读
专注于IT技术分析

Python 3基础快速入门介绍

python由Guido van Rossum于1990年代初期开发, 最新版本为3.7.1, 我们可以简单地将其称为Python3。 Python 3.0于2008年发布。它是一种解释语言, 即未编译, 解释器将逐行检查代码。本文可以用来学习以下方面的基础知识Python程序设计语言.

因此, 在继续之前..让我们做最流行的” HelloWorld”传统hence, 然后将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的基础知识。

简介和设置

如果你在

Windows操作系统

下载Python

点击这里

然后从安装程序安装, 并在开始菜单中输入IDLE.IDLE, 你可以将其视为运行Python脚本的Python IDE。

看起来会这样:

Python 3基础1

如果你在

类似于Linux/Unix

只需打开终端并在99%的Linux操作系统上预安装Python操作系统即可。只需在终端中键入” python3″, 即可开始使用。

它看起来像这样:

Python 3基础2

” >>>”表示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

看, 它有多简单, 只需创建一个变量并为其分配所需的任何值, 然后使用打印功能即可将其打印出来。 Python有4种类型的内置数据结构, 即list, 字典, tuple和设置。

list

是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]

评论:

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

选拔

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

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

输出如下:

hello
hello again
hello
hello 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()

输出如下:

Started
Enter integer: 5

迭代(循环)

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

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

输出如下:

0
1
2
3
4

模组

Python具有非常丰富的模块库, 该库具有用于执行许多任务的多种功能。你可以通过以下方式阅读有关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主题。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

赞(0)
未经允许不得转载:srcmini » Python 3基础快速入门介绍

评论 抢沙发

评论前必须登录!