Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Pythoncode

Uploaded by

padhyeshaunak
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Pythoncode

Uploaded by

padhyeshaunak
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import sys

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)

#determining values as true or false


#FALSE:
print(bool(False))
print(bool(None))
print(bool(0))
print(bool(""))
print(bool(()))
print(bool([]))
print(bool({}))
#TRUE:
print(bool("abc"))
print(bool(123))
print(bool(["apple", "cherry", "banana"]))
#exponent
x = 2
y = 5
print(x ** y) #same as 2*2*2*2*2
x = 15
y = 2
print(x // y)
#the floor division // rounds the result down to the nearest whole number
#PYHTON ASSIGNMENT OPERATORS
x = 5
x *= 3
print(x)
y = 7
y //= 3
print(y)
x = 5
x **= 3
print(x)
#PYTHON COMPARISON OPERATORS
x = 5
y = 3
print(x == y) #equal to
# returns False because 5 is not equal to 3
x = 5
y = 3
print(x != y)#not equal to
# returns True because 5 is not equal to 3
x = 5
print(x > 3 and x < 10) # and
# returns True because 5 is greater than 3 AND 5 is less than 10
x = 5
print(x > 3 or x < 4)
# returns True because one of the conditions are true (5 is greater than 3, but 5
is not less than 4)
x = 5
print(not(x > 3 and x < 10))
# returns False because not is used to reverse the result

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

You might also like