String Concatenation and Replication Polymorphism means many forms The and

String concatenation and replication polymorphism

This preview shows page 8 - 12 out of 18 pages.

String Concatenation and Replication Polymorphism means many forms. The + and * operator in Python are somewhat polymorphic in that they can take on more than one form. + means addition when the two operands are ints or floating point numbers. + means concatenation (putting together) when the two operands are strings. >>> ‘ alice ’ + ‘bob’ results in a single string – alicebob >>> ‘ alice ’ + 42 results in an error (cannot convert ‘ int ’ object to a Str object implicitly)
Image of page 8
String Concatenation and Replication * means multiplication when the operands are ints or floating point numbers * means String replication when the operands are one string and one int value. (String replication will not work with a floating point number) alice ’ * 3 results in ‘ alicealicealice
Image of page 9
Storing Values in Variables A variable is a memory location where you can store a single value. = is the assignment operator. ( Assignment is read from R to L.) variableA = 40 A variable is said to be initialized (created) the first time a value is stored in it. Once initialized, a variable can be used in expressions with other variables and literals. When a variable is assigned a new value, the old value is lost. (This is know as overwriting the variable)
Image of page 10
Variable Names Hyphens are not allowed to be used in variable names.
Image of page 11
Image of page 12

You've reached the end of your free preview.

Want to read all 18 pages?

  • Fall '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture