Skip to main content

Posts

web-content

 https://github.com/chavalibala6-source/web-content
Recent posts

python operator

  Python provides an if statement with the same semantics as languages like C++ and Java, although with its own sparse syntax: if expression1: suite1 elif expression2: suite2 else: suite3 The first thing that stands out to programmers used to C++ or Java is that there are no parentheses and no braces. The other thing to notice is the colon: This is part of the syntax and is easy to forget when starting out. Colons are used with else, elif, and in many other places to indicate that a block of code (a suite in Python-speak) is to follow. As we would expect, there can be any number. Group Operators Description Comparison <, <=, ==, !=, >=, > The <> operator is also permitted as a synonym for != but is deprecated Identity is, is not These are used to determine if two object references refer to the same underlying object Membership in, not in These are used on lists, dictionaries, and strings Logical not, and,or Both and and or short-circuit; the bit-wise equivalents a...

python basics: effective python ref

pantry = [ ('avocados', 1.25), ('bananas', 2.5), ('cherries', 15), ] for i, (item, count) in enumerate(pantry): print('#%d: %-10s = %.2f' % (i, item, count)) What It Prints (Correctly) #0: avocados = 1.25 #1: bananas = 2.50 #2: cherries = 15.00 #%d → prints the index number (0, 1, 2). %-10s → left-aligns the string (item name) in a field 10 characters wide. Example: "bananas " (adds spaces to align neatly). %.2f → prints the number with 2 decimal places. So the = sign belongs in the format string itself: '%-10s = %.2f' ✅ If you run the exact code above, you will not see separate lines with just the numbers. Instead, you’ll see neatly aligned item + count pairs. 👉 Do you want me to also show you the f-string version (more modern & cleaner than % formatting)? yes Perfect 👍 f-strings make this much cleaner and easier to read. Here’s your same example rewritten with f-strings: pantry = [ ('av...

comparision

Example Input file1.txt shard1 host1: 6379 host2: 6379 host3: 6379 shard2 host4: 6379 host5: 6379 host6: 6379 file2.txt shard1 host1: 6379 host2: 6379 host3: 6379 shard2 host4: 6379 host5: 9999 host6: 6379 Python script to compare def parse_file ( filename ): """Read shard -> hosts mapping from file.""" shards = {} with open (filename, "r" ) as f: for line in f: parts = line.strip().split() if not parts: continue shard = parts[ 0 ] hosts = parts[ 1 :] shards[shard] = set (hosts) # use set for easy comparison return shards def compare_shards ( file1, file2 ): shards1 = parse_file(file1) shards2 = parse_file(file2) all_shards = set (shards1.keys()) | set (shards2.keys()) for shard in sorted (all_shards): hosts1 = shards1.get(shard, set ()) hosts2 = shards2.get(shard, set ())...