1 / 10

Variables and Assignment

Variables and Assignment. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Computing and Memory. All computing in terms of memory locations Storing values into locations in memory Reading values from locations in memory

seoras
Download Presentation

Variables and Assignment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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)

More Related