<jupyter_start><jupyter_text>Python:example command of python Numbers: Integers and floats work as you would expect from other languages:<jupyter_code>x = 1    # int
y = 2.5  # float
z = 1j   # complex

print(type(x)) 
print(type(y))
print(type(z))
print(x)       # Prints "3"
print(x + 1)   # Addition; prints "4"
print(x - 1)   # Subtraction; prints "2"
print(x * 2)   # Multiplication; prints "6"
print(x ** 2)  # Exponentiation; prints "9"
x += 1
print(x)  # Prints "4"
x *= 2
print(x)  # Prints "8"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"<jupyter_output><class 'int'>
<class 'float'>
<class 'complex'>
1
2
0
2
1
2
4
2.5 3.5 5.0 6.25<jupyter_text>Booleans: Python implements all of the usual operators for Boolean logic, but uses English words rather than symbols (&&, ||, etc.):<jupyter_code>t = True
f = False
print(type(t)) # Prints "<class 'bool'>"
print(t == f)
print(t and f) # Logical AND; prints "False"
print(t or f)  # Logical OR; prints "True"
print(not t)   # Logical NOT; prints "False"
print(t != f)  # Logical XOR; prints "True"


s = "Hello, World!"
print(s[7:])
print(s.capitalize())  # Capitalize a string; prints "Hello"
print(s.upper())       # Convert a string to uppercase; prints "HELLO"
print(s.rjust(7))      # Right-justify a string, padding with spaces; prints "  hello"
print(s.center(7))     # Center a string, padding with spaces; prints " hello "
print(s.replace('l', '(ell)'))  # Replace all instances of one substring with another;
                                # prints "he(ell)(ell)o"
print('  world '.strip())  # Strip leading and trailing whitespace; prints "world"
a=2
print(f"{a} hi {s}")<jupyter_output>World!
Hello, world!
HELLO, WORLD!
Hello, World!
Hello, World!
He(ell)(ell)o, Wor(ell)d!
world
2 hi Hello, World!<jupyter_text>ContainersPython includes several built-in container types: lists, dictionaries, sets, and tuples. ListsA list is the Python equivalent of an array, but is resizeable and can contain elements of different types:<jupyter_code>xs = [3, 1, 2]    # Create a list
print(xs, xs[2])  # Prints "[3, 1, 2] 2"
print(xs[-1])     # Negative indices count from the end of the list; prints "2"
xs[2] = 'foo'     # Lists can contain elements of different types
print(xs)         # Prints "[3, 1, 'foo']"
xs.append('bar')  # Add a new element to the end of the list
print(xs)         # Prints "[3, 1, 'foo', 'bar']"
x = xs.pop()      # Remove and return the last element of the list
print(x, xs)      # Prints "bar [3, 1, '
nums = list(range(5))     # range is a built-in function that creates a list of integers
print(nums)               # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4])          # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:])           # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:2])           # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(nums[:])            # Get a slice of the whole list; prints "[0, 1, 2, 3, 4]"
print(nums[:-1])          # Slice indices can be negative; prints "[0, 1, 2, 3]"
nums[2:4] = [8, 9]        # Assign a new sublist to a slice
print(nums) 
print(len(nums))<jupyter_output>[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]
5<jupyter_text>Tuples<jupyter_code>tup1 = ("apple", "banana", "cherry")
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
print(tup1)
print(tup2)
print(tup3)
len(tup1)
tup1[0]
print(tup1+tup2)
print(tup1[-1])
print(tup1[0]+tup1[1])<jupyter_output>('apple', 'banana', 'cherry')
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd')
('apple', 'banana', 'cherry', 1, 2, 3, 4, 5)
cherry
applebanana<jupyter_text>ListList is a collection which is ordered and changeable. Allows duplicate members. Python tuples vs lists – Mutability<jupyter_code>fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[1])
print(fruits[-1])
print(fruits[2:5])
fruits[1] = "blackcurrant"
print(fruits)
if "apple" in fruits:
  print("Yes, 'apple' is in the fruits list")
fruits.append("orange")
print(fruits)
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print(squares)



list3 = fruits + squares
print(list3)<jupyter_output>banana
mango
['cherry', 'orange', 'kiwi']
['apple', 'blackcurrant', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
Yes, 'apple' is in the fruits list
['apple', 'blackcurrant', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'orange']
[0, 1, 4, 9, 16]
['apple', 'blackcurrant', 'cherry', 'orange', 'kiwi', 'melon', 'mango', 'orange', 0, 1, 4, 9, 16]<jupyter_text>Set: unordered and unindexed. No duplicate members.<jupyter_code>animals = {'cat', 'dog'}
print('cat' in animals)   # Check if an element is in a set; prints "True"
print('fish' in animals)  # prints "False"
animals.add('fish')       # Add an element to a set
print('fish' in animals)  # Prints "True"
print(len(animals))       # Number of elements in a set; prints "3"
animals.add('cat')        # Adding an element that is already in the set does nothing
print(len(animals))       # Prints "3"
animals.remove('cat')     # Remove an element from a set
print(len(animals))       # Prints "2"<jupyter_output>True
False
True
3
3
2<jupyter_text>Dictionary: unordered, changeable and indexed. No duplicate members.<jupyter_code>thisdict = {
  "brand": "AUDI",
  "model": "Q5",
  "year": 2016
}
print(thisdict)
for x in thisdict:
  print(thisdict[x])
for x in thisdict.values():
  print(x)
for x, y in thisdict.items():
  print(x, y)<jupyter_output>brand AUDI
model Q5
year 2016<jupyter_text>Function<jupyter_code>def my_function(fname, lname):
  print(fname + " " + lname)

my_function("Farnoosh", "Khodakarami")
def factorial(a):
  fact = 1
  if a > 0:
    for i in range(1,a+1):
      fact = i * fact
  return fact

print(factorial(5))<jupyter_output>120<jupyter_text>Numpy<jupyter_code>import numpy as np

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"

a = np.array([[6, 1, 1],
              [4, -2, 5],
              [2, 8, 7]])
 
print(a.shape)            # Prints "(3,)"
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # Change an element of the array
print(a)                  # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"<jupyter_output><class 'numpy.ndarray'>
(3, 3)
[6 1 1] [ 4 -2  5] [2 8 7]
[[ 5  5  5]
 [ 4 -2  5]
 [ 2  8  7]]
(2, 3)
1 2 4<jupyter_text>Pandas<jupyter_code>#import the pandas library and aliasing as pd
import pandas as pd
df = pd.DataFrame()
print(df)
# Import pandas package
import pandas as pd
import numpy as np
 
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Age':[27, 24, 22, 32],
        'City':['Toronto', 'Ottawa', 'Calgary', 'Toronto'],
        'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
 
# Convert the dictionary into DataFrame 
df = pd.DataFrame(data)
print(df)
# select two columns
print(df[['Name', 'Qualification']])
df[["Age","City"]]

df = pd.DataFrame([[np.nan, 2, np.nan, 0],
                  [3, 4, np.nan, 1],
                  [np.nan, np.nan, np.nan, 5],
                  [np.nan, 3, np.nan, 4]],
                  columns=list('ABCD'))
df
df.fillna(1)<jupyter_output><empty_output><jupyter_text>We can also propagate non-null values forward or backward.<jupyter_code>print(df)
df.fillna(method='bfill',axis='columns')
values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
df.fillna(value=values)
df.fillna(df.mean()).dropna(axis=1)

df.dropna(axis="columns")
#df.dropna(inplace=True)
#df
data1 = {'Name':['Jai', 'Jack', 'Gaurav', 'Amin'],
        'Age':[27, 24, 22, 32],
        'City':['Toronto', 'Ottawa', 'Calgary', 'Toronto'],
        'Qualification':['Msc', 'MA', np.nan, 'Phd']}
 
data2 = {'Name':['Jai', 'Jack', 'Gaurav', 'Amin'],
        'Grade':['A', 'B', 'A', 'C+']
        }
# Convert the dictionary into DataFrame 
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
pd.merge(df1,df2, on='Name', how='inner')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup