Guide to use the Online Python Compiler.
- Write the code in the editor main file.
- Click on the Run button to execute the code.
- The output for Python IDE code can be seen on the right-hand side window.
- To use the Python console, click the arrow button to select the Python console from the drop-down menu.
- To add a CSV, text file, or JSON file, click the + plus button icon: Paste the data and name the CSV or data file.
Online Python compiler code examples.
Python Variable
x = 456 y = "Variable" z = 'Alpha' print(x, y , z)
Output
456 Variable Alpha
Python Data Type
# Set String value (str data type) to a variable
a_str = "I am a string data type value"
print(a_str)
# Set Integer value (int data type) to a variable
a_int = 41
print(a_int)
# Set Float value (float data type) to a variable
a_float = 53.5
print(a_float)
# Set Complex value (complex data type) to a variable
a_complex = 5j
print(a_complex)
# Set List value (list data type) to a variable
a_list = ["Asia", "America", "Africa"]
print(a_list)
# Set Tuple value (tuple data type) to a variable
a_tuple = ("Asia", "America", "Africa")
print(a_tuple)
# Set Range value (Range data type) to a variable
a_range = range(11)
print(a_range)
# Set Dictionary value (dict data type) to a variable
a_dict = {"Employee_id": 70923, "Employee_name": "Jonathan"}
print(a_dict)
# Set SET value (set data type) to a variable
a_set = {"Asia", "America", "Africa"}
print(a_set)
# Set frozenset value (frozenset data type) to a variable
a_frozenset = frozenset({"Asia", "America", "Africa"})
print(a_frozenset)
# Set Boolean value (bool data type) to a variable
a_bool = True
print(a_bool)
# Set Bytes value (bytes data type) to a variable
a_bytes = b"Hello"
print(a_bytes)
# Set Bytearray value (bytearray data type) to a variable
a_bytearray = bytearray(5)
print(a_bytearray)
# Set memoryview value (memoryview data type) to a variable
a_memoryview = memoryview(bytes(5))
print(a_memoryview)
# Use type() function to return class type of the argument(object) passed as parameter.
print(type(a_str))
print(type(a_int))
print(type(a_float))
print(type(a_complex))
print(type(a_list))
print(type(a_tuple))
print(type(a_range))
print(type(a_dict))
print(type(a_set))
print(type(a_frozenset))
print(type(a_bool))
print(type(a_bytes))
print(type(a_bytearray))
print(type(a_memoryview))
Output
I am a string data type value
41
53.5
5j
['Asia', 'America', 'Africa']
('Asia', 'America', 'Africa')
range(0, 11)
{'Employee_id': 70923, 'Employee_name': 'Jonathan'}
{'America', 'Asia', 'Africa'}
frozenset({'America', 'Asia', 'Africa'})
True
b'Hello'
bytearray(b'\x00\x00\x00\x00\x00')
<memory at 0x000001D274D291C8>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'range'>
<class 'dict'>
<class 'set'>
<class 'frozenset'>
<class 'bool'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>
Python Casting
a = int(input()) b = a + 3 print(b)
Output
4 7
Python String
single_quote_variable = 'Hi I am placed inside single quotes' double_quote_variable = "Hi I am placed inside double quotes" print(single_quote_variable) print(double_quote_variable)
Output
Hi I am placed inside single quotes Hi I am placed inside double quotes
Python Boolean Operators
a = 3 b = 4 print(a == b) print(a != b) print(a > b) print(a < b) print(a >= b) print(a <= b)
Output
False True False True False True
Python Lists
Nordic_Country_List = ["FINLAND", "ICELAND", "NORWAY", "DENMARK", "SWEDEN", "FAROE ISLANDS"]
Asian_Country_List = ["CHINA", "INDIA", "INDONESIA", "PAKISTAN", "BANGLADESH", "JAPAN", "PHILIPPINES", "VIETNAM"]
for i in Nordic_Country_List:
print(i, end=" ")
print("\n")
for i in Asian_Country_List:
print(i, end=" ")
print("\n")
#Lets calculate the size of the List
#Then we access element of List using index
Nordic_length = len(Nordic_Country_List)
Asian_length = len(Asian_Country_List)
for i in range(Nordic_length):
print(Nordic_Country_List[i], end=" ")
print("\n")
for i in range(Asian_length):
print(Asian_Country_List[i], end=" ")
Output
FINLAND ICELAND NORWAY DENMARK SWEDEN FAROE ISLANDS CHINA INDIA INDONESIA PAKISTAN BANGLADESH JAPAN PHILIPPINES VIETNAM FINLAND ICELAND NORWAY DENMARK SWEDEN FAROE ISLANDS CHINA INDIA INDONESIA PAKISTAN BANGLADESH JAPAN PHILIPPINES VIETNAM
Python Tuples
a = (1, 2, 3) # 1Dimensional TUPLE of order 1 by 3
b = (5, 6, 6) # 1Dimensional TUPLE of order 1 by 3
c = (102, 5, -7) # 1Dimensional TUPLE of order 1 by 3
x1 = ((1, 2, 4), (2, 4, 5), (0, 0, 0)) # 2Dimensional TUPLE of order 3 by 3
x2 = ((5, 6, 7), (11, 14, 45), (90, 87, 73)) # 2Dimensional TUPLE 3 by 3
x3 = ((7, 11, 19), (15, 154, 415), (190, 807, 723)) # 2Dimensional TUPLE 3 by 3
d = (a, b, c) # 2Dimensional TUPLE of order 3 by 3
#D1 D2
D1 = len(d)
D2 = len(d[0])
for i in range(D1):
for j in range(D2):
print(d[i][j], end=" ")
print("\t")
print("\n")
x = [x1, x2, x3] # 3Dimensional TUPLE of order 3 by 3 by 3
#X1 X2
X1 = len(x)
X2 = len(x[0])
X3 = len(x[0][0])
for i in range(X1):
for j in range(X2):
for k in range(X3):
print(x[i][j][k], end=" ")
print("\t")
print("\n")
Output
1 2 3 5 6 6 102 5 -7 1 2 4 2 4 5 0 0 0 5 6 7 11 14 45 90 87 73 7 11 19 15 154 415 190 807 723
Python Sets
a = {1, 2, 3}
for i in a:
print(i)
a.add(4)
print(a)
#We are adding list as well as set to a set
#We are also trying to add duplicate element 1 to existing set
a.update([5, 6, 7], {1, 9, 10})
print(a)
Output
1
2
3
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 7, 9, 10}
Python Dictionaries
#When Keys are used as integers
#You can access Dictionary value using indexing
sample_dict = {0: 'pink', 1: 'red'}
print(sample_dict)
print(sample_dict[0])
print(sample_dict[1])
print(sample_dict.get(0))
print(sample_dict.get(1))
sample_dict = {'name': 'Pythonbaba', "Role": ["Programmer", "Teacher", "Writer"]}
print(sample_dict)
print(sample_dict["name"])
print(sample_dict.get("name"))
print(sample_dict["Role"])
print(sample_dict["Role"][0])
print(sample_dict.get(["Role"][0]))
print(sample_dict["Role"][1])
print(sample_dict["Role"][0:1])
print(sample_dict["Role"][1:2])
print(sample_dict["Role"][0:2])
print(sample_dict["Role"][:])
Output
{0: 'pink', 1: 'red'}
pink
red
pink
red
{'name': 'Pythonbaba', 'Role': ['Programmer', 'Teacher', 'Writer']}
Pythonbaba
Pythonbaba
['Programmer', 'Teacher', 'Writer']
Programmer
['Programmer', 'Teacher', 'Writer']
Teacher
['Programmer']
['Teacher']
['Programmer', 'Teacher']
['Programmer', 'Teacher', 'Writer']
Python If..Else
a = int(input("Enter first integer: "))
b = int(input("Enter Second integer: "))
if a < b:
print("{} is the lowest integer".format(a))
else:
print("{} is the lowest integer".format(b))
Output
Neither a is greater than b Nor b is equal to c
Python While Loops
a = [0, 1, 2, 3, 4]
for i in a:
a[i] += 1
print("List after increment: {}".format(a))
Output
List after increment: [1, 2, 3, 4, 5]
Python For Loops
a = [0, 1, 2, 3, 4]
for i in a:
a[i] += 1
print("List after increment: {}".format(a))
Output
List after increment: [1, 2, 3, 4, 5]
Python Functions
def addition_fun(a, b):
return a + b
def multiply_fun(a, b):
return a * b
def subtraction_fun(a, b):
return a - b
def division_fun(a, b):
return a / b
x = int(input("Enter any integer value: "))
y = int(input("Enter any integer value: "))
print("Result after Addition is {}".format(addition_fun(x, y)))
print("Result after Multiplication is {}".format(multiply_fun(x, y)))
print("Result after Subtraction is {}".format(subtraction_fun(x, y)))
print("Result after Division is {}".format(division_fun(x, y)))
Output
Enter any integer value: 10 Enter any integer value: 5 Result after Addition is 15 Result after Multiplication is 50 Result after Subtraction is 5 Result after Division is 2.0