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)
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 ’
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)
Variable Names Hyphens are not allowed to be used in variable names.
You've reached the end of your free preview.
Want to read all 18 pages?
- Fall '19