Pythoncode
Pythoncode
print(sys.version)
a, b, c=4, 5.7, "hi"
print(type(a))
print(type(b))
print(type(c))
fruits=["apple", "banana", "strawberry"]
x, y, z= fruits
print(x)
print(type(fruits))
a=9
b=10
def function():
a=4
b=10
print(a+b)
function()
print(a+b)
x = "awesome"
def myfunc():
global x #global x should be inside a funtion, not outside it
x = "amazing"
myfunc()
print("Python is " + x)
#converting one data type to another
def func():
a=30 #int
b=40.67 #float
c=15+9j #complex
x= float(a)
y= int(b)
print(x, y)
func()
import random
print (random.randrange(80,100))
#pyhton casting
f=int(9.8)
print(f)
g=float(3)
print(g)
h=complex(84)
print(h)
#multiline strings - three quotation marks
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
#strings as arrays
a = "Hello, World!"
print(a[7]) #'H' is 0
#looping through a string
for x in "banana":
print(x)
#string length
a = "Hello, World!"
print(len(a))
#check string
txt = "The best things in life are free!"
print("free" in txt)
print("cost" in txt)
if "free" in txt:
print("Yes, 'free' is present.")
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
#slice
b = "Hello, World!"
print(b[4:13])
a = "Hello"
b = "World"
c = a + " " + b
print(c) #concatenate strings
a = " Hello, World!"
print(a.upper()) #upper case
print(a.lower())
print(a.strip()) # returns "Hello, World!"
print(a.replace("H", "J")) # REPLACE
"""
age = 36
txt = "My name is John, I am " + age
print(txt)
""" # we cannot combine strings and numbers like this
#hence use f-strings
age=15
txt=f"My name is Shaunak, I am {age}"
print(txt)
txt = f"The price is {20 * 59} dollars"
print(txt)
#with f-string, use {} curly brackets!
txt = "We are the so-called \"Vikings\" from the north."
#the escape character (\) allows you to use double quotes when you normally would
not be allowed
print(txt)
txt = "Hello\nWorld!" # (\n) means new line
print(txt)
#BOOLEANS:
x = 30
y= 40
if x>y:
txt=f"{x} is greater than {y}"
print(txt)
else:
txt=f"{y} is greater than {x}"
print(txt)
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if they have the same
content
print(x == y)
# to demonstrate the difference betweeen "is" and "==": this comparison returns
True because x is equal to y