{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "8818bde1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gavaskar,India,10122,3092\n", "Tendulkar,India,15921,18426\n", "Dravid,India,13288,10889\n" ] } ], "source": [ "f = open('players.csv', 'r')\n", "for line in f:\n", " line = line.strip() # whitespace around the line\n", " fields = line.split(',')\n", " if fields[1] == 'India':\n", " print(line)" ] }, { "cell_type": "code", "execution_count": 15, "id": "3c00db18", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gavaskar\tIndia 10122 3092\n", "Tendulkar India 15921 18426\n", "Dravid India 13288 10889\n" ] } ], "source": [ "f = open('players.txt', 'r')\n", "for line in f:\n", " line = line.strip() # whitespace around the line\n", " #print(line)\n", " fields = line.split()\n", " if fields[1] == 'India':\n", " print(line)" ] }, { "cell_type": "code", "execution_count": 23, "id": "a4af1a94", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "player country testrun odirun\n", "1\n", "Gavaskar India 10122 3092\n", "1\n", "Border Aus 11174 6524\n", "1\n", "Waugh Aus 10927 7569\n", "1\n", "Lara WI 11953 10405\n", "1\n", "Chanderpaul WI 11219 8778\n", "1\n", "Jayawardene SL 11319 11549\n", "1\n", "Tendulkar India 15921 18426\n", "1\n", "Ponting Aus 13378 13704\n", "1\n", "Kallis SA 13289 11574\n", "1\n", "Dravid India 13288 10889\n", "1\n", "Sangakkara SL 11151 12548\n" ] } ], "source": [ "f = open('players.txt', 'r')\n", "for line in f:\n", " line = line.strip() # whitespace around the line\n", " #print(line)\n", " fields = line.split('\\n')\n", " print(len(fields))\n", " print(fields[0])\n", " #if fields[1] == 'India':\n", " # print(line)" ] }, { "cell_type": "code", "execution_count": 24, "id": "1300936c", "metadata": {}, "outputs": [], "source": [ "a,b = 1,2" ] }, { "cell_type": "code", "execution_count": 25, "id": "2e1cf34c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 26, "id": "f2e55862", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 27, "id": "d17182d8", "metadata": {}, "outputs": [], "source": [ "b,a = a,b" ] }, { "cell_type": "code", "execution_count": 28, "id": "db58f29e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 29, "id": "2add22a3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 30, "id": "98422ecd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 31, "id": "774502cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(b))" ] }, { "cell_type": "code", "execution_count": 32, "id": "079e3950", "metadata": {}, "outputs": [], "source": [ "c = a + b" ] }, { "cell_type": "code", "execution_count": 33, "id": "ddf14ca4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(c))" ] }, { "cell_type": "code", "execution_count": 34, "id": "49bf3f80", "metadata": {}, "outputs": [], "source": [ "d = a.__add__(b)" ] }, { "cell_type": "code", "execution_count": 35, "id": "760d1489", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 36, "id": "95c2c4be", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 37, "id": "f12b0c5c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(d))" ] }, { "cell_type": "code", "execution_count": 38, "id": "46672797", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__abs__',\n", " '__add__',\n", " '__and__',\n", " '__bool__',\n", " '__ceil__',\n", " '__class__',\n", " '__delattr__',\n", " '__dir__',\n", " '__divmod__',\n", " '__doc__',\n", " '__eq__',\n", " '__float__',\n", " '__floor__',\n", " '__floordiv__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getnewargs__',\n", " '__getstate__',\n", " '__gt__',\n", " '__hash__',\n", " '__index__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__int__',\n", " '__invert__',\n", " '__le__',\n", " '__lshift__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__neg__',\n", " '__new__',\n", " '__or__',\n", " '__pos__',\n", " '__pow__',\n", " '__radd__',\n", " '__rand__',\n", " '__rdivmod__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rfloordiv__',\n", " '__rlshift__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__ror__',\n", " '__round__',\n", " '__rpow__',\n", " '__rrshift__',\n", " '__rshift__',\n", " '__rsub__',\n", " '__rtruediv__',\n", " '__rxor__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__sub__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__trunc__',\n", " '__xor__',\n", " 'as_integer_ratio',\n", " 'bit_count',\n", " 'bit_length',\n", " 'conjugate',\n", " 'denominator',\n", " 'from_bytes',\n", " 'imag',\n", " 'is_integer',\n", " 'numerator',\n", " 'real',\n", " 'to_bytes']" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(a)" ] }, { "cell_type": "code", "execution_count": 39, "id": "3b89387b", "metadata": {}, "outputs": [], "source": [ "p_list = ['Perl', 'Python', 'Ruby']" ] }, { "cell_type": "code", "execution_count": 41, "id": "d3bb9424", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Perl\n", "1 Python\n", "2 Ruby\n" ] } ], "source": [ "for index in list(range(len(p_list))):\n", " print(index, p_list[index])" ] }, { "cell_type": "code", "execution_count": 71, "id": "1ad654cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(len(p_list))" ] }, { "cell_type": "code", "execution_count": 69, "id": "69f3f84b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 67, "id": "f71f871c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Perl\n", "Python\n", "Ruby\n" ] } ], "source": [ "for index in range(len(p_list)):\n", " print(p_list[index])" ] }, { "cell_type": "code", "execution_count": 43, "id": "3a3da385", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Perl\n", "Python\n", "Ruby\n" ] } ], "source": [ "for v in p_list:\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 44, "id": "b2995212", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Perl\n", "1 Python\n", "2 Ruby\n" ] } ], "source": [ "for i,v in enumerate(p_list):\n", " print(i, v)" ] }, { "cell_type": "code", "execution_count": 45, "id": "394a420a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Perl\n", "1 Python\n", "2 Ruby\n" ] } ], "source": [ "for index in list(range(len(p_list))):\n", " print(index, p_list[index])" ] }, { "cell_type": "code", "execution_count": 46, "id": "f5f1101b", "metadata": {}, "outputs": [], "source": [ "given = ['Sachin','Rahul','Sourav','VVS']\n", "family = ['Tendulkar','Dravid', 'Ganguly','Laxman']" ] }, { "cell_type": "code", "execution_count": 47, "id": "6496af3a", "metadata": {}, "outputs": [], "source": [ "players = {}" ] }, { "cell_type": "code", "execution_count": 48, "id": "2f1c426e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(players))" ] }, { "cell_type": "code", "execution_count": 49, "id": "e204aee0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Sachin\n", "1 Rahul\n", "2 Sourav\n", "3 VVS\n" ] } ], "source": [ "for i,v in enumerate(given):\n", " print(i,v)" ] }, { "cell_type": "code", "execution_count": 50, "id": "04b08282", "metadata": {}, "outputs": [], "source": [ "for i,v in enumerate(given):\n", " players[v] = family[i]" ] }, { "cell_type": "code", "execution_count": 51, "id": "34c9dc5c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Sachin': 'Tendulkar',\n", " 'Rahul': 'Dravid',\n", " 'Sourav': 'Ganguly',\n", " 'VVS': 'Laxman'}" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "players" ] }, { "cell_type": "code", "execution_count": 52, "id": "823f0128", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zip(given, family)" ] }, { "cell_type": "code", "execution_count": 53, "id": "111ad4a0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('Sachin', 'Tendulkar'),\n", " ('Rahul', 'Dravid'),\n", " ('Sourav', 'Ganguly'),\n", " ('VVS', 'Laxman')]" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(zip(given, family))" ] }, { "cell_type": "code", "execution_count": 54, "id": "c1d72011", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Sachin': 'Tendulkar',\n", " 'Rahul': 'Dravid',\n", " 'Sourav': 'Ganguly',\n", " 'VVS': 'Laxman'}" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(zip(given, family))" ] }, { "cell_type": "code", "execution_count": 55, "id": "4f55699d", "metadata": {}, "outputs": [], "source": [ "name = 'Anil'" ] }, { "cell_type": "code", "execution_count": 57, "id": "f99f73e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key Anil does not exist\n" ] } ], "source": [ "if name in players:\n", " f = players[name]\n", "else:\n", " print ('key %s does not exist' % name)" ] }, { "cell_type": "code", "execution_count": 60, "id": "ca65aaa2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "try:\n", " f = players['Sachin']\n", "except KeyError:\n", " traceback.print_exc()\n", "finally:\n", " print('Hello world!')" ] }, { "cell_type": "code", "execution_count": 1, "id": "69082829", "metadata": {}, "outputs": [], "source": [ "import pickle as pk\n", "\n", "a = {'key1': (1,2,3, 'world', [1,2,3]), \n", " 'key2': {'b': 0, 'c': -1, 'd': 11}}\n", "b = {'a': 1, 'b': 2}\n", "\n", "f = open('store.pk', 'wb')\n", "pk.dump(a,f)\n", "pk.dump(b,f)\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 2, "id": "a556ea58", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'key1': (1, 2, 3, 'world', [1, 2, 3]), 'key2': {'b': 0, 'c': -1, 'd': 11}}\n" ] } ], "source": [ "f = open('store.pk', 'rb') \n", "a = pk.load(f)\n", "print(a)\n", "f.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }