SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Variables and Assignment in Python Programming

seoras avatar
10 diapositives · 248 Views · Jul 27, 2014

In this overview of variables and assignment in Python programming, we explore the fundamentals of storing and managing data in memory. Learn how to read and write values into memory locations and convert temperature from Celsius to Fahrenheit using variables. Discover the rules for naming variables, the importance of namespaces, and the use of expressions and operators in assignments. Understand how functions work and how to combine values effectively. This guide will enhance your programming skills and help you grasp the integral concepts of data manipulation in Python.

Understanding Variables and Assignment in Python Programming - slide 1
01 / 10Understanding Variables and Assignment in Python Programming
1 / 17

Télécharger la présentation

Understanding Variables and Assignment in Python Programming
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. Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1

  2. Computing and Memory • All computing in terms of memory locations • Storing values into locations in memory • Readingvalues from locations in memory • Example: Converting Celsius temperature to Fahrenheit • Get Celsius temp from user, store in location 1 • Compute 1.8 x value in location 1, add 32, store in location 2 • Output value in location 2 to user

  3. Variables • Variable name given to location in memory • Example: Converting Celsius temperature to Fahrenheit • Get Celsius temp from user, store in celsius_temperature • Compute 1.8 x celsius_temperature+ 32, store in fahrenheit_temperature • Output value of fahrenheit_temperatureto user

  4. Namespaces • Python environment / operating system maps variables to locations in memory • Namespace: list of variables and corresponding values currently active in Python environment • Variable added to namespace when first used • Other languages require variable to be declared first • Can use id(variable) function to find actual numeric id Python uses for variable

  5. Naming Variables • Rules (different in other languages) • Must start with letter or underscore • Rest may contain letters, underscores, and digits • Cannot contain spaces or other characters • Case sensitive • Cannot be Python keyword • Should be descriptive (readability)

  6. Assignment • Storing values into variables • Syntax: variable = expression • Expression evaluates to a value • That value stored in the variable • Not same as = in math!x = 7 assigns the value 7 to x7 = x not legal!

  7. Expressions • Expressions (on RHS of assignment) may contain: • Literals (fixed values) • Numbers 2, 3.5, -12.6, etc. • Strings (must be inside quotes) “John” • Booleans True, False • Variables • Must have already been assigned a value! • Functions • Must return a value

  8. Functions • Predefined code with action based on list of arguments • Syntax: functionname(list of arguments) • multiple arguments separated by commas • May return a value (so can be part of expression) • Examples: • print(list of strings) Displays strings separated by spaces • input(prompt) Displays prompt, returns value entered by user • id(variable) Returns numeric id of variable in namespace

  9. Expressions and Operators • Variables, literals, functions can be combined using operators • Most are binary operators combining 2 operands • Exception: unary minus sign -12 • Syntax: operandoperator operand • Example: Can append two strings using +string3 = string1 + string2

  10. Example # Prompt the user for their first and last name first_name = input(“What is your first name? ”) last_name= input(“What is your last name? ”) # Combine the two into a full name # (separated by a space) full_name = first_name + “ “ + last_name # Display greeting to user with their full name print(“Hello”, full_name)