* Gearhead columnist Mark Gibbs digs deeper into the language of Python Last week we looked at the language Python (https://www.nwfusion.com/columnists/2003/0526gearhead.html), and this week we’ll dig deeper. But first, a letter from reader Greg Martin, who wrote: “You might mention that Python program structure is all based on white space. This is kind of cool, but requires you to pay attention! There are no constructs so in the following code: if condition do this now move on The ‘do this’ will get executed if condition is true and ‘now move on’ always will get executed. Also, the iterative loop construct is wild. Python iterates over lists, which can be a lot of things. But if you want to iterate i from 1 to 10 you need to build a list using the range function, ‘For i in range(10):’ “Thanks, Greg. This white-space thing – otherwise called ‘indentation’ – really does require your attention! Each statement in Python is terminated by a new line or a semicolon (“;”) and if you need to span multiple lines you can use the line-continuation character, “”. Thus: if x: statement1 statement2else: statement3 statement4 Now the “if” clause above is fine because the indentation is consistent, but Python will gag on the “else” clause because of the extra indent before statement4.On the other hand, line-continuations aren’t required for triple-quoted strings (strings enclosed by a set of three quotes at both ends specifically for spanning multiple lines), lists, tuples (lists that can’t be modified or extended with new elements after creation) and dictionaries (associative arrays that pair keys with values). Some examples:fruit = [ “Orange”, # a list – note that indexes start at 0 “Lemon”, #and this one is spread over “Apple” # multiple lines ]layout = (“Bin1”, “Bin2”, “Bin3”) # a tuple on a single line fruitsection = { “Orange : “Bin1”, # a dictionary with three keys “Lemon” : “Bin2”, # on multiple lines “Apple” : “Bin3” }You could also define fruitsection as: fruitsection = { fruit[0] : layout[0], fruit[1] : layout[1], fruit[2] : layout[2] }Note that in the last example we’ve compressed the layout of the dictionary definition block to a single line. Want to change the fruitsection dictionary? fruitsection [“Pear”] = “Bin5” # add a new member fruitsection [“Apple”] = “Bin7” # change a member del shop [“Pear”] # delete a memberPython has a rich set of methods (functions) for manipulating variables, lists, tuples and dictionaries. And you can create compound lists: shop = [“hat”, fruitsection, [12.3, “gearhead”, 6]]The reference shop [1][“Lemon”] would result in “Bin2”, while shop[1][2] would fail because fruitsection is a dictionary. On the other hand, shop[2][2] would return 6.Also note that in Python you don’t have to declare variables before using them and can change the type of a variable by assigning a new value to it of a different type: mynumber = 10 # mynumber is an integer multiplier = 1.5 # multiplier is a floating-point mynumber = mynumber * multiplier #mynumber is now a floating-pointPython also supports long integers and complex numbers.Our final piece of Python for this week concerns loops. There are several types of loops you can use: while test: dosomethingOr: for n in range(1,10): dosomethingNote that Greg used the shortened form for range, which actually loops from 0 to the value given (also see the xrange() function in the Python documentation). Finally, you can use: for a in “Hello world”: # you can use strings, lists, tuples and dictionaries print aYour thoughts to gearhead@gibbs.com Related content news Broadcom to lay off over 1,200 VMware employees as deal closes The closing of VMware’s $69 billion acquisition by Broadcom will lead to layoffs, with 1,267 VMware workers set to lose their jobs at the start of the new year. By Jon Gold Dec 01, 2023 3 mins Technology Industry Mergers and Acquisitions news analysis Cisco joins $10M funding round for Aviz Networks' enterprise SONiC drive Investment news follows a partnership between the vendors aimed at delivering an enterprise-grade SONiC offering for customers interested in the open-source network operating system. By Michael Cooney Dec 01, 2023 3 mins Network Management Software Network Management Software Network Management Software news Cisco CCNA and AWS cloud networking rank among highest paying IT certifications Cloud expertise and security know-how remain critical in building today’s networks, and these skills pay top dollar, according to Skillsoft’s annual ranking of the most valuable IT certifications. Demand for talent continues to outweigh s By Denise Dubie Nov 30, 2023 7 mins Certifications Certifications Certifications news Mainframe modernization gets a boost from Kyndryl, AWS collaboration Kyndryl and AWS have expanded their partnership to help enterprise customers simplify and accelerate their mainframe modernization initiatives. By Michael Cooney Nov 30, 2023 4 mins Mainframes Cloud Computing Data Center Podcasts Videos Resources Events NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe