This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #1.Using % operator | |
| calc = 22.3765+8.00003422-(411%35) #here % works as a caculation operator | |
| print("Result of calculation is %3.2f"%calc) #here % interpolates a floating point number | |
| print("\nHey! I'm %s, %d years old and I love %s Programing"%('Emma',33,'Python')) #values are replaced from a tuple | |
| format_str="\nHey! I'm %s and I'm %d years old." | |
| emma_info=('Emma',33) | |
| print(format_str %emma_info) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #2.Using .format() method | |
| #Part-1 | |
| name="Emma" | |
| age=33 | |
| lan="Python" | |
| #default | |
| print("\nHey! I'm {}, {} years old, and I love {} Programming.".format(name,age,lan)) | |
| #using indexes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #2.Using .format() method | |
| #Part-2 | |
| #Dictionary | |
| person_dict= dict(name="Emma", age=33, country="UK", lan="Python") | |
| print("Hey! My name is {name}, I'm {age} years old, currently living in {country} and love {lan} programming" | |
| .format(**person_dict)) # '**' - used for mapping | |
| #List | |
| person_list=["Emma",33,"UK"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #3.Using f-string formatting | |
| #Part-1 | |
| from datetime import datetime | |
| my_name="Emma" | |
| today=datetime.now() | |
| #List | |
| info=["Emma",33,"UK","Python"] | |
| print(f"{info[0]} is {info[1]} years old currently living in {info[2]} and loves {info[3]} programming.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #4.Using Template string | |
| from string import Template | |
| #creating object of Template class | |
| person_info=Template('\n$name is $years years old and loves $lan programming!') | |
| print(person_info.substitute(name='Emma',years=33,lan='Python')) #substitute() | |
| List1=[("Emma",33,"Python"),("Harry",34,"Java")] | |
| person_data=Template("\n$age years old $name loves $lan programming") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #3.Using f-string formatting | |
| #Part-2 | |
| class Person: | |
| """Returns the information about person.""" | |
| def __init__(self, n, a, l): | |
| """Initializing person details.""" | |
| self.name = n | |
| self.age = a | |
| self.prog_lan = l |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #1.Directly by listing all its members/elements. | |
| num_cube=[8,64,216,512,1000] | |
| print(f"num_cube[] = {num_cube}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #2.Using for loop. | |
| num_cube_floop=[] | |
| for n in range(1,11): | |
| if n%2 == 0: | |
| num_cube_floop.append(n**3) | |
| print(f"num_cube_floop[] = {num_cube_floop}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #3.Using '+=' operator. | |
| num_cube_op=[] | |
| with open("list_input.txt","r") as input_data: | |
| for i in input_data: | |
| num_cube_op += i.split() | |
| print(f"num_cube_op[] = {num_cube_op}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 8 | |
| 64 | |
| 216 | |
| 512 | |
| 1000 |
OlderNewer