Skip to content

Instantly share code, notes, and snippets.

View PhoenixIM's full-sized avatar

Iffat Malik PhoenixIM

  • United Kingdom
View GitHub Profile
@PhoenixIM
PhoenixIM / string_interpolation_modulo.py
Last active June 9, 2020 12:03
string interpolation using %
#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)
@PhoenixIM
PhoenixIM / string_interpolation_format()_1.py
Last active June 10, 2020 12:15
string interpolation using .format()
#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.
@PhoenixIM
PhoenixIM / string_interpolation_format()_2.py
Last active June 9, 2020 16:25
string interpolation using .format()
#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"]
@PhoenixIM
PhoenixIM / string_interpolation_Fstring.py
Last active June 10, 2020 09:56
string interpolation using f-string
#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.")
@PhoenixIM
PhoenixIM / string_interpolation_Template.py
Created June 8, 2020 21:14
string interpolation using Template class
#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")
@PhoenixIM
PhoenixIM / f_string_class_obj.py
Last active June 10, 2020 09:55
string interpolation using f-string and class-object
#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
@PhoenixIM
PhoenixIM / list1.py
Created July 26, 2020 09:07
creating a list by enumerating its elements
#1.Directly by listing all its members/elements.
num_cube=[8,64,216,512,1000]
print(f"num_cube[] = {num_cube}")
@PhoenixIM
PhoenixIM / list2.py
Created July 26, 2020 09:12
creating a list using for loop
#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}")
@PhoenixIM
PhoenixIM / list3.py
Created July 26, 2020 09:14
creating a list using +=
#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}")
8
64
216
512
1000