Docsity

fundamentos de phyton, Resúmenes de Programación Informática

Vista previa parcial del texto

¡Descarga fundamentos de phyton y más Resúmenes en PDF de Programación Informática solo en Docsity!

15-110: Principles of

Computing

Basic Elements of Python Programs

Lecture 2, September 04, 2018

Mohammad Hammoud

Carnegie Mellon University in Qatar

Today…

  • (^) Last Session:
    • Motivation
    • (^) Introduction to Hardware Basics & Programming Languages
    • Writing Simple Python Commands
  • (^) Today’s Session:
    • (^) Basic Elements of Python Programs: Literals, Assignments, Datatype Conversion, Identifiers, and Expressions
  • Announcement:
    • (^) We will practice on the Python basic elements on Thursday, September 06, 2018 during the recitation
  • (^) A literal is used to indicate a specific value, which can be assigned to a variable >>> x = 2 >>> print(x) 2 >>> x = 2. >>> print(x) 2. Simple Assignment Statements  (^) x is a variable and 2 is its value
  • (^) A literal is used to indicate a specific value, which can be assigned to a variable >>> x = 2 >>> print(x) 2 >>> x = 2. >>> print(x) 2. Simple Assignment Statements  (^) x is a variable and 2 is its value  (^) x can be assigned different values; hence, it is called a variable
  • (^) Python assignment statements are actually slightly different from the “variable as a box” model - (^) In Python, values may end up anywhere in memory, and variables are used to refer to them

x = 2 print(x) 2 x = 2. print(x)

Simple Assignment Statements: Actual View 2 Before x = 2. 2 After x x

What will happen to value 2?

  • (^) Interestingly, as a Python programmer you do not have to worry about computer memory getting filled up with old values when new values are assigned to variables
  • (^) Python will automatically clear old values out of memory in a process known as garbage collection

Garbage Collection

After x

X

Memory location will be automatically reclaimed by the garbage collector

  • (^) Here is a sample interaction with the Python interpreter:
  • (^) Notice that whatever the user types is then stored as a string
    • (^) What happens if the user inputs a number? Assigning Input

name = input("Enter your name: ") Enter your name: Mohammad Hammoud name 'Mohammad Hammoud'

  • (^) Here is a sample interaction with the Python interpreter:
  • (^) How can we force an input number to be stored as a number and not as a string? - (^) We can use the built-in eval function, which can be “wrapped around” the input function Assigning Input

number = input("Enter a number: ") Enter a number: 3 number '3'

Still a string!

  • (^) Here is a sample interaction with the Python interpreter: Assigning Input

number = eval(input("Enter a number: ")) Enter a number: 3. number

And now a float (no single quotes)!

  • (^) Here is another sample interaction with the Python interpreter: Assigning Input

number = eval(input("Enter an equation: ")) Enter an equation: 3 + 2 number

5

The eval function will evaluate this formula and return a value, which is then assigned to the variable “number”

  • (^) Besides, we can convert the string output of the input function into an integer or a float using the built-in int and float functions Datatype Conversion >>> number = float(input("Enter a number: ")) Enter a number: 3. >>> number 3. >>> A float (no single quotes)!
  • (^) As a matter of fact, we can do various kinds of conversions between strings, integers and floats using the built-in int , float , and str functions Datatype Conversion

x = 10 float(x)

str(x) '10'

y = "20" float(y)

int(y) 20

z = 30. int(z) 30 str(z) '30.0'

integer  float integer  string string  float string  integer float  integer float  string

  • (^) Suppose you have two variables x and y, and you want to swap their values ( i.e., you want the value stored in x to be in y and vice versa )

Simultaneous Assignment

x = 2 y = 3 x = y y = x x

3

y 3

X

CANNOT be done with two simple assignments

  • (^) Suppose you have two variables x and y, and you want to swap their values ( i.e., you want the value stored in x to be in y and vice versa )

Simultaneous Assignment

x = 2 y = 3 temp = x x = y y = temp x 3 y 2

CAN be done with three simple assignments, but more efficiently with simultaneous assignment Thus far, we have been using different names for variables. These names are technically called identifiers