Python教程

Python基础

本文主要是介绍Python基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

0. 垃圾

记录一些没见过的、与其他语言不同的新特性

1. 代码块

  • Python依靠缩进数判断代码块(而不是大括号)。
  • 一组连续的、缩进数相同的逻辑行被编译器判断为一个代码块。
  • 在编程时,代码块不能随意创建。
# In python, the indentation means a new code block
print("Hi")
    print("I am Joey") #error
#Cannon start a new code block arbitrarily

 2. 函数

  • 使用def function_name(parameters): 定义函数
  • 接下来使用缩进来创建代码块
def test_function(a,b):
    a=a+10
    b=b+10
    if(a<b):
        print(b, "wins!")
    else:
        print(a, "wins!")

3. 全局变量

  • 在函数中使用全局变量可以给处在该代码块上层的变量赋值

 

global globalV
def test_global():
    global globalV
    globalV = 45
    print("now globalV is", globalV)

 

这篇关于Python基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!