Tuesday, January 28, 2020

Today's python learning: Cascaded Assignments, Simultaneous assignment, Multiline statement, Multiline strings, split() method and eval method

Today I learned:

Cascaded Assignments:

We can assign the same value to multiple variables at the same time. For example, if we write

x=y=z=10

there will be 3 separate variables each of them holding 10 in their content. We can see from there content. This variable type and content explorer can be viewed from the Spyder IDE, which is cool and intuitive.

Simultaneous assignment:

We can also do simultaneous assignments in Python which is not commonly available to other languages. To do that we have to use a comma between expressions and an equal (assignment) operator at the right side. The following variable swapping problem is a perfect example of a simultaneous assignment.

We can see from the output that it works perfectly.

Multiline Statement:

This can be constructed using either escaping a newline character or using the parentheses.

Multiline String:

The multiline string can be done using enclosing strings into either a set of triple-double quotation marks or single quotation marks.

Follow the following examples for clarification.

Split method:

This is a very important method. It returns a list of strings after breaking the given string by the specified separator It accepts two parameters. Separator and the Max split. The following is an example of the split method, where the default separator is space.


eval() function:

This is a powerful tool as it evaluates the strings into python expressions. So we need to be careful about this tool. It will be an amazingly handy tool for building a calculator app or some graphing tools. The example can be found below.







1 comment:

  1. Mango Habanero Casino Coupons - JTM Hub
    With the latest 안양 출장샵 Mango Habanero 영주 출장샵 Coupons, shoppers can save on your next purchase with the 공주 출장안마 code "Mango Habanero". 청주 출장마사지 Get 30% off all casino promo 하남 출장샵 code

    ReplyDelete