Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Similar presentations


Presentation on theme: "Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1."— 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 – Reading values from locations in memory Example: Converting Celsius temperature to Fahrenheit 1.Get Celsius temp from user, store in location 1 2.Compute 1.8 x value in location 1, add 32, store in location 2 3.Output value in location 2 to user

3 Variables Variable  name given to location in memory Example: Converting Celsius temperature to Fahrenheit 1.Get Celsius temp from user, store in celsius_temperature 2.Compute 1.8 x celsius_temperature + 32, store in fahrenheit_temperature 3.Output value of fahrenheit_temperature to 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 x 7 = 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” BooleansTrue, 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: operand operator 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)


Download ppt "Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1."

Similar presentations


Ads by Google