Python Multiple Lines

Single line with more than 79 characters can be written in multiple lines

Line continuation character (\) to represent the line continuation.
total = item_one + \
        item_two + \
        item_three

Method definition or invoking part is more than 79 characters

Method definition or invoking part is more than 79 characters, then that line can be written in multiple lines as below
source_file = os.path.join(config_directory,  "temp_dir",                                 
                           "file1.txt")   
Example:
#!/usr/bin/python                                                               
import os                                                                       
                                                                                
config_directory = "test/";                                                     
source_file = os.path.join(config_directory,                                    
                           "file1.txt")                                         
dest_file = os.path.join(config_directory,                                      
                         "file2.txt")                                           
                                                                                
def get_file_data(file_name):                                                   
    if not os.path.exists(file_name):                                           
        return ''                                                               
                                                                                
    try:                                                                        
        with open(file_name,                                                    
                  'r') as f:                                                    
            return f.read()                                                     
    except IOError:                                                             
        print("Error reading file: "                                            
              "%s" % file_name);                                                
        return ''                                                               
                                                                                
file_data = get_file_data(source_file) + "\n" + \                               
            "New line in code";                                                 
                                                                                
with open(dest_file, 'w') as df:                                                
    df.write(file_data);                                                        
os.chmod(dest_file, 0o600);    
Output:
$ python test_file_write.py 
$ cat test/file2.txt 
line1
line2

New line in code

Array initialization in multiple lines

[], {}, or () brackets do not need to use the line continuation character.
list_var = ['one', 'two', 'three',
           'four', 'five'];
Example:
#!/usr/bin/python                                                               
                                                                                
list_var = ['one', 'two', 'three',                                              
            'four', 'five'];                                                    
list_var.append('six');                                                         
                                                                                
new_list_var = ['seven', 'eight'];                                              
list_var.extend(new_list_var);                                                  
                                                                                
print list_var;   
Output:
$ python test_array.py 
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']

Multiple statements in single line

Semicolon ( ; ) can be used for multiple statements on the single line.
import sys;sys.exit();

Multiple assignment statements in single line?

Multiple assignments in single line.
a = b = c = 1

Multiple assignment statements with different objects in single line

Assigning multiple objects to multiple variables in single line.
a, b, c = 1, 2, "test"

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^