va variables
play

Va Variables When creating a variable in Python, no type is - PowerPoint PPT Presentation

1/29/20 Va Variables When creating a variable in Python, no type is provided x = 20 s = You think youve lost your love pi = 3.14159 So what happens if I do this (after the statements above)? x = pi CS 224


  1. 1/29/20 Va Variables • When creating a variable in Python, no type is provided • x = 20 • s = ‘You think youve lost your love’ • pi = 3.14159 • So what happens if I do this (after the statements above)? • x = pi CS 224 Introduction to Python Spring 2020 • Answer: x has the value 3.14159 Class #02: Variables, Expressions, and Statements 1

  2. 1/29/20 Bu Built-in in types Ty Types • boolean • Python has types – what determines the type of an object? • values True and False (note capitalization) • Duck typing • Numeric types: int, float, long, complex • if it walks like a duck and quacks like a duck… • Sequence types: str, list, tuple • set • So on previous slide, x was an int until we reassigned it. Then it became a float. • dict • type(x) reports the type of variable x 2

  3. 1/29/20 Tr Truth values Type Casting Ty • Syntax: new_type(object) • Any object can be used in a Boolean expression • The following evaluate to False • None x = 3.14159 • False y = 20 • 0 • empty sequences: ’’, [], () s = ‘20’ • empty dictionary: {} int(x) 3 20.0 float(y) ‘3.14159’ str(x) 3

  4. 1/29/20 Ty Type Casting continued Operator Precedence x = 3.14159 • No surprises • parentheses y = 20 • exponentiation s = ‘20’ • multiplication/division • addition/subtraction t = ‘111’ 20 int(s) • “I don’t work very hard to remember rules for other operators. If I error int(y) can’t tell by looking at the expression, I use parentheses to make it obvious.” ‘3.14159’ str(x) 7 int(t, 2) int(t, 8) 73 4

  5. 1/29/20 String Operators • We will see string methods later. Here are a couple of useful operators. • + string concatenation • s1 = ‘This is ‘ • s2 = ‘a test.’ • print s1 + s2 • * string repeat • s = ‘spam ‘ • print s * 4 + ‘eggs, bacon, and spam’ 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend