SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Variable Scope in Python Functions

dean-hart avatar
12 diapositives · 225 Views · Nov 13, 2014

Learn about variable scope in Python functions, how names live within functions, and see examples of local and global variables.

Understanding Variable Scope in Python Functions - slide 1
01 / 12Understanding Variable Scope in Python Functions
1 / 17

Télécharger la présentation

Understanding Variable Scope in Python Functions
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Functions … return values # sample code def gross_pay (pay_per_hour, hours_worked): pay = pay_per_hour * hours_worked line of code pretax_pay = gross_pay(10.50, 10.0) print (pretax_pay) What if line of code is: What is returned?Type? return pay return is empty,i.e. no return

  2. Variable Scope …where do your names live? • variable scope – where the variable is created, changed, or accessed • when we only had a main program, • there was only one namespace • all variable were in that one namespace • when we create a function, • the function has its own namespace • variable assigned in the function can only be seen by the code in that function

  3. Variable Scope …where do your names live? def cube (x):      value = x * x * x      return value value = 25 cubed = cube(2) print (cubed, value)

  4. Variable Scope …where do your names live? def clear_count(count): count = 0 return order_num = 15 clear_count(order_num) print(order_num)

  5. Variable Scope …where do your names live? def clear_count(count): count = 0 return count = 15 clear_count(count) print(count)

  6. Variable Scope …where do your names live? topping = "whipped cream“ def order_ice_cream(flavour): topping = flavour return order_ice_cream("chocolate") print (topping)

  7. Variable Scope …where do your names live? topping = "whipped cream“ def order_ice_cream(flavour): print("Mmm,", flavour, "with", topping) return order_ice_cream("chocolate")

  8. Variable Scope …where do your names live? If main and functions in same file: If main and functions in different files: variable inside function is local to the function variables inside the main are not visible to the functions separate files make good fences (enforces no peeking) • variable inside function is local to the function • variables inside the main are visible to the functions • be a good neighbour, do not peek.

  9. Calculate area and volume of cylinder [A3, Q1] PI = 3.1416 radius = float(input(“Enter cylinder radius: “)) height = float(input(“Enter cylinder height: “)) area = PI * radius**2 volume = area * height print(“Cylinder area = {:.2f}”.format(area)) print(“Cylinder volume = {:.2f}”.format(volume))

  10. PI = 3.1416 radius = float(input(“Enter cylinder radius: “)) height = float(input(“Enter cylinder height: “)) print(“Cylinder area = {:.2f}”.format(area)) print(“Cylinder volume = {:.2f}”.format(volume))

  11. Lumens given bulb wattage [A6, Q1] • watts = int(input(“Enter watts: “)) • if watts == 15: • lumens = 125 • elif watts == 25: • lumens = 215 • elif watts == 40: • lumens = 500 • elif watts == 60: • lumens = 880 • elif watts == 75: • lumens = 1000 • elif watts == 100: • lumens = 1675 • else: • lumens = None • print() • if lumens == None: • print(“Invalid wattage”) • else: • print(“For a bulb wattage of {} watts, brightness is {} lumens” .format(watts, lumens))

  12. Lumens given bulb wattage [A6, Q1] • if watts == 15: • lumens = 125 • elif watts == 25: • lumens = 215 • elif watts == 40: • lumens = 500 • elif watts == 60: • lumens = 880 • elif watts == 75: • lumens = 1000 • elif watts == 100: • lumens = 1675 • else: • lumens = None • watts = int(input(“Enter watts: “)) • print() • if lumens == None: • print(“Invalid wattage”) • else: • print(“For a bulb wattage of {} watts, brightness is {} lumens” .format(watts, lumens))