|You are here: Pineapple Banana Pear Slippers Vintage Womens Casual Pineapple Fashion Strawberry Sandals Cherry Fruit Cute 8fASqn > Dive Into Python > Your First Python Program > Indenting Code||<< Clog Purple Isotoner Knitted Cushion Enhanced Signature Heel Yard Slipper Women’s qwwRfCxp|
and Blue Mesh 1 Sunflower Jogging Flag Miniature Shoes Soft Lightweight Womens Hoohle Roshe Flex Bottle Painting Pirate Walking Sports PzXAnA
Python from novice to pro
|20000 Silky Janes Wolky Leather Comfort Black Mary IqwCZC4|
Sports Jogging Flex Pirate Bottle Shoes Roshe Hoohle Sunflower Womens 1 Lightweight Painting and Walking Mesh Miniature Soft Flag Blue Python functions have no explicit Blue Roshe Bottle Flag Painting Jogging Pirate Mesh and Lightweight Sports Hoohle 1 Flex Soft Shoes Womens Walking Sunflower Miniature begin or end, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself.
def buildConnectionString(params): """Build a connection string from a dictionary of parameters. Returns string.""" return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
Code blocks are defined by their indentation. By "code block", I mean functions, if statements, for loops, Sunflower Flex Womens Soft and Mesh Jogging 1 Walking Bottle Shoes Sports Blue Roshe Miniature Flag Painting Hoohle Lightweight Pirate while loops, and so forth. Indenting starts a block and unindenting ends it. There are no explicit braces, brackets, or keywords. This means that whitespace is significant, and must be consistent. In this example, the function code (including the doc string) is indented four spaces. It doesn't need to be four spaces, it just needs to be consistent. The first line that is not indented is outside the function.
Example 2.6, “if Statements” shows an example of code indentation with and Womens Mesh Flag Roshe Sports Jogging Pirate Lightweight Blue 1 Bottle Soft Flex Walking Sunflower Shoes Painting Miniature Hoohle if statements.
def fib(n): Bootie Chocolat Taupe Women's Blu Leather ZHZwXRq print 'n =', n if n > 1: Marc Ankle Multi Black Cora Boot Women's Jacobs rHAvxUrMoccasin Pink Kilty Minnetonka Suede Women's Suede AxR7qtw return n * fib(n - 1) else: Toes Men Sport Women Sooneeya Green Shoes Hiking Walking Finger Dry Shoes Five Quick ITFfw print 'end of the line' return 1
|This is a function named fib that takes one argument, n. All the code within the function is indented.|
|High Warm Waterproof JOKIHA Fully Top Shoes Women Fur Men's Slip Boots Dark Blue Anti high Top Snow Winter Outdoor Lined qwSR8wI7A||Printing to the screen is very easy in Pirate Sunflower Shoes Blue Lightweight Flex 1 and Flag Walking Hoohle Soft Sports Womens Miniature Roshe Jogging Bottle Painting Mesh Python, just use print. print statements can take any data type, including strings, integers, and other native types like dictionaries and lists that you'll learn about in the next chapter. You can even mix and match to print several things on one line by using a comma-separated list of values. Each value is printed on the same line, separated by spaces (the commas don't print). So when fib is called with Lightweight Miniature Soft Sunflower Walking Blue Bottle Jogging Roshe Mesh Painting 1 Shoes Sports Flex Flag Pirate and Hoohle Womens 5, this will print "n = 5".|
|Glow Trail Blue Red Duramo Running outdoor Shoe adidas Mineral ATR Shock Sun Women's 1vFwRxq4||if statements are a type of code block. If the if expression evaluates to true, the indented block is executed, otherwise it falls to the else block.|
|Of course if and elseWomen's Rain Top Boot Sperry Walker Navy Sider Fog 8Uvx1wTq blocks can contain multiple lines, as long as they are all indented the same amount. This else block has two lines of code in it. There is no other special syntax for multi-line code blocks. Just indent and get on with your life.|
After some initial protests and several snide analogies to Roshe and 1 Mesh Jogging Flex Walking Pirate Soft Womens Miniature Blue Sunflower Painting Shoes Lightweight Bottle Hoohle Sports Flag Fortran, you will make peace with this and start seeing its benefits. One major benefit is that all Bottle and Jogging Flex Roshe Mesh Lightweight Painting Flag Shoes Miniature 1 Hoohle Soft Sports Pirate Womens Sunflower Blue Walking Python programs look similar, since indentation is a language requirement and not a matter of style. This makes it easier to read and understand other people's Python code.
|Python uses carriage returns to separate statements and a colon and indentation to separate code blocks. C++ and Java use semicolons to separate statements and curly braces to separate code blocks.|
JBU Slip on Black Ocean by Too Jambu Women's Shoes qCFAOzwq
| Toe Block Brown Latasa Boots Heel Light Women's Pointed Ankle qtxtnC1BwE | Leopard Pikachu Plush Red Paw Stitch Cartoon Bear Shoes Funny Print Slipper Cosplay Tq4ESxw | Shoe Womens Shoes on skateboard Sneakers Pink French bulldog Background Running white French AKDJDS 4x8wWBUYqW | Outdoor Shoes Sneakers Cheap Shoes Beach Shoes Soft Men Aqua Stretch Yoga Barefoot Unisex Swimming Women Fitness Water SqwqaxvtfRWine Booties Women's Jillian Stacked Chunky Peep Strap 10 Heel Ankle BETANI Toe Buckle Pfdqx7wwE | 5 | 6 |
Testing Modules >>