Python Basics
Comments
Operators
Variables & Data Types
Input, Indentation & Whitespaces
Comments
Comments
Comments
Comments
Comments
Comments
Comments
Comments
#print(“Kishore”) #This is a comment. print("Hello, World!") #This is a comment """ This is a comment written in more than just one line """
Comparison Operators
print(10 >= 9) print(10 == 9) print(10 <= 9) print(10 != 10) a = 200 b = 333 if b > a: print("b is greater than a")Output True False False False b is greater than a
Variables & Data Types
Name = "kishore" # Name is Variable & kishore is Value Age = 35 Height = 5.6 Locations = [“Chennai", “Bangalore", “Goa"] print(Name) print(type(Name)) print(type(Age)) print(type(Height)) print(type(Locations)) print(bool()) print(bool(Age)) Casting = str(3) print(type(Casting))Output kishore class 'str'> class 'int'> class 'float'> class 'list'> False True class 'str'>
Input, Indentation & Whitespaces
name = input("Enter your name: ") print("Hello,", name) user_input = input("Enter something with spaces: ") print("Original input:", repr(user_input)) # Removing leading/trailing whitespace trimmed_input = user_input.strip() print("Trimmed input:", repr(trimmed_input)) if b > a: print("b is greater than a")Get only Names to ListOutput Enter your name: kishore Hello, kishore Enter something with spaces: kumar Original input: 'kumar ' Trimmed input: 'kumar' IndentationError: expected an indented block after 'if' statement
ami_list =[] for Images in In['Images']: #print(Images['CreationDate']) ami_list.append(Images['Name']) print('ami_list',ami_list) print(len(ami_list))Get only CreationDate to List & Latest CreationDateOutput ami_list ['Jenkins Master 2 Backup', 'AD-CCC-SAML-21-08-20-02-51-pm', 'AD-CCC-SAML-21-08-20-02-51-pm'] 3
creation_dates = [image['CreationDate'] for image in In['Images']] latest_creation_dates = max(creation_dates) print('creation_dates',creation_dates) print('latest_creation_dates',latest_creation_dates)Get Latest Creation Image DictionaryOutput creation_dates ['2021-09-20T05:25:47.000Z', '2023-08-21T09:21:31.000Z', '2022-08-21T09:21:31.000Z'] latest_creation_dates 2023-08-21T09:21:31.000Z
new = max(image['CreationDate'] for image in In['Images'] if image['Name'].startswith('AD-CCC-SAML')) print('new',new) # Step 1 TO Get Latest Creation Image Dictionary filtered_images = [image for image in In['Images'] if image['CreationDate'] == new] print('filtered_images',filtered_images) # Step 2 TO Get Latest Creation Image Dictionary for Images in In['Images']: if Images['CreationDate'] == new: print(Images) else : print('no')Check Latest Date StringStep 1 Output filtered_images [{'Architecture': 'x86_64', 'CreationDate': '2023-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '750344256621/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '750344256621', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'}] Step 2 Output no {'Architecture': 'x86_64', 'CreationDate': '2023-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '750344256621/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '750344256621', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'} no
a = '2022-08-20T05:21:47.000Z' b = '2022-08-22T05:21:47.000Z' if a > b: print('a greater') elif a < b: print('b greater') else: print('a = b')Get 'b'List non matching numbers from 'a'ListOutput b greater
a = [2,3,4,5,6,7,8,9,0] b = [0,12,4,6,242,7,9] for x in (x for x in b if x not in a): print(x)Compare Both Lists and Get matching valuesOutput 12 242
a = [2,3,4,5,6,7,8,9,0] xyz = [0,12,4,6,242,7,9] for x in filter(lambda w: w in a, xyz): print (x)Output 0 4 6 7 9