Python: Local variables and global variables

Source: Internet
Author: User
Tags variable scope
#!usr/bin/env python
#-*-Coding:utf-8-*-
#除了字符串和整数, the other can be modified in the function
#在子程序中定义的变量称为局部变量, a variable defined at the beginning of a program is called a global variable
#全局变量作用域是整个程序, and the local variable scope is the subroutine that defines the variable

__author__ = "Samson"
School = "Oldboy"
def change_name (name):
Global school# is defined as a globals, and the school is the same as the fifth row school
School = "Edu"
Print ("Before changed", name)
Name = Name.title () #这个函数就是这个变量的作用域 (local variable)
Print ("After changed", name)

Name = "Samson"
Change_name (name)
Print (name)

names = ["Alex", "Samson"]
def change ():
Names[0] = "Sam"
Change ()
Print (names)

Python: Local variables and global variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.