0% found this document useful (0 votes)
14 views13 pages

Tempcodeng

The document covers various Python programming concepts like variables, datatypes, strings, lists, tuples, dictionaries and sets. It includes examples to add numbers, find remainder, check variable type, compare values, calculate average, accept user input, format strings, store and sort lists, check tuple immutability, sum a list, count elements in a tuple and get dictionary keys.

Uploaded by

iamtushar1140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Tempcodeng

The document covers various Python programming concepts like variables, datatypes, strings, lists, tuples, dictionaries and sets. It includes examples to add numbers, find remainder, check variable type, compare values, calculate average, accept user input, format strings, store and sort lists, check tuple immutability, sum a list, count elements in a tuple and get dictionary keys.

Uploaded by

iamtushar1140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

{

"cells": [
{
"cell_type": "markdown",
"id": "57ad5291",
"metadata": {},
"source": [
"### 2. Variables and Datatypes"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bb9cac96",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter 1st Number: 15\n",
"Enter 2nd Number: 20\n",
"35\n"
]
}
],
"source": [
"# Add 2 numbers:\n",
"\n",
"a = int(input(\"Enter 1st Number: \"))\n",
"b = int(input(\"Enter 2nd Number: \"))\n",
"print(a+b)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3a2694f4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter Number: 49\n",
"1\n"
]
}
],
"source": [
"# Find remainder when x is divided by 2.\n",
"\n",
"x = int(input(\"Enter Number: \"))\n",
"print(x%2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "aedd1dce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter anything: 35\n",
"<class 'str'>\n"
]
}
],
"source": [
"# Check type of variable assigned using input() function:\n",
"\n",
"var = input(\"Enter anything: \")\n",
"print(type(var))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "93729f1d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"# using comparison operator check if a is greater than b given that a = 34 and
b = 80.\n",
"\n",
"a = 34\n",
"b = 80\n",
"if a>b:\n",
" print(True)\n",
"else:\n",
" print(False)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c66e4f61",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter 1st Number: 28\n",
"Enter 2nd Number: 56\n",
"42.0\n"
]
}
],
"source": [
"# Average of 2 numbers entered by the user.\n",
"\n",
"a = int(input(\"Enter 1st Number: \"))\n",
"b = int(input(\"Enter 2nd Number: \"))\n",
"avg = (a+b)/2\n",
"print(avg)"
]
},
{
"cell_type": "markdown",
"id": "eb6a3a0e",
"metadata": {},
"source": [
"### 3. Strings:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e98a8c6f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your Name: Tushar\n",
"Good Afternoon, Tushar\n"
]
}
],
"source": [
"# Display user entered name followed by Good Afternoon using input()
function.\n",
"\n",
"name = input(\"Enter your Name: \")\n",
"print('Good Afternoon,', name)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6c7cf216",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your Name: Tushar Shekhar\n",
"Enter Date: 29-03-2024\n",
"Dear, Tushar Shekhar\n",
" You are selected, Congratulations\n",
"29-03-2024\n"
]
}
],
"source": [
"# Replace letter template by user entered name and date.\n",
"\n",
"name = input(\"Enter your Name: \")\n",
"date = input('Enter Date: ')\n",
"\n",
"letter = \"\"\"Dear, <|NAME|>\n",
" You are selected, Congratulations\n",
"<|DATE|>\"\"\"\n",
"\n",
"letter = letter.replace(\"<|NAME|>\", name)\n",
"letter = letter.replace(\"<|DATE|>\",date)\n",
"print(letter)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "86b2ec1e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"# Program to detect double spaces in a string\n",
"\n",
"st = \"Hello, My name is Tushar Shekhar. \"\n",
"print(st.find(\" \"))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a5afa842",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, My name is Tushar Shekhar. \n"
]
}
],
"source": [
"# Replace double space from above code into single space.\n",
"\n",
"st = \"Hello, My name is Tushar Shekhar. \"\n",
"print(st.replace(\" \",\" \"))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "6fac55c9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dear Harry,\n",
"\tThis Python course is nice.\n",
"Thanks!\n"
]
}
],
"source": [
"# using escape sequence characters format the given letter.\n",
"\n",
"letter_esc = \"Dear Harry,\\n\\tThis Python course is nice.\\nThanks!\"\n",
"print(letter_esc)"
]
},
{
"cell_type": "markdown",
"id": "a091dcd9",
"metadata": {},
"source": [
"### 4. Lists and Tuples."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "469b6da7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter name of fruit: Apple\n",
"Enter name of fruit: Mango\n",
"Enter name of fruit: Banana\n",
"Enter name of fruit: Kiwi\n",
"Enter name of fruit: Strawberry\n",
"Enter name of fruit: Grapes\n",
"Enter name of fruit: Oranges\n",
"['Apple', 'Mango', 'Banana', 'Kiwi', 'Strawberry', 'Grapes', 'Oranges']\n"
]
}
],
"source": [
"# Store 7 fruits entered by user in the list.\n",
"\n",
"fruit_li = []\n",
"for i in range(7):\n",
" x = input(\"Enter name of fruit: \")\n",
" fruit_li.append(x)\n",
"print(fruit_li)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "cb345331",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter marks: 25\n",
"Enter marks: 15\n",
"Enter marks: 34\n",
"Enter marks: 26\n",
"Enter marks: 28\n",
"Enter marks: 27\n",
"[15, 25, 26, 27, 28, 34]\n"
]
}
],
"source": [
"# Accept marks of 6 students and display them in sorted orded.\n",
"\n",
"marks_li = []\n",
"for i in range(6):\n",
" x = int(input(\"Enter marks: \"))\n",
" marks_li.append(x)\n",
"\n",
"marks_li.sort()\n",
"print(marks_li)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "560bcf7e",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\
u001b[1;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[1;32mIn[17], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m \
u001b[38;5;66;03m# Check that a tuple cannot be changed in python.\u001b[39;00m\n\
u001b[0;32m 3\u001b[0m tup \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m2\
u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m6\u001b[39m,\
u001b[38;5;241m8\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mData\
u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\
u001b[38;5;124mAnalyst\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;241m10\
u001b[39m)\n\u001b[1;32m----> 4\u001b[0m tup[\u001b[38;5;241m2\u001b[39m] \
u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mScience\
u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item
assignment"
]
}
],
"source": [
"# Check that a tuple cannot be changed in python.\n",
"\n",
"tup = (2,4,6,8,\"Data\",\"Analyst\",10)\n",
"tup[2] = \"Science\"\n",
"# Gives error as tuples are immutable in python."
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "ee34c4ee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"# Sum a list with 4 numbers.\n",
"\n",
"add_li = [2,4,6,8]\n",
"add = 0\n",
"for i in range(len(add_li)):\n",
" add = add + add_li[i]\n",
"print(add)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "019ce460",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"# count number of zeros in given tuple.\n",
"\n",
"a = (7,0,8,0,0,9)\n",
"print(a.count(0))"
]
},
{
"cell_type": "markdown",
"id": "4d8508a7",
"metadata": {},
"source": [
"## 5. Dictionary and Sets:\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4f7c4069",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Options: dict_keys(['Kursi', 'Mez', 'Bistar', 'Chabi'])\n",
"Enter Hindi word from:Bistar\n",
"The Meaning of Bistar is Bed\n"
]
}
],
"source": [
"# Hindi english dict with look up:\n",
"\n",
"Hdic = {\n",
" 'Kursi':'Chair',\n",
" 'Mez':'Table',\n",
" 'Bistar':'Bed',\n",
" 'Chabi':'Keys'\n",
"}\n",
"\n",
"print(\"Options:\",Hdic.keys())\n",
"lookup = input(\"Enter Hindi word from:\")\n",
"print(\"The Meaning of\",lookup,\"is\",Hdic[lookup])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b4474bc8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter Number: 45\n",
"Enter Number: 54\n",
"Enter Number: 65\n",
"Enter Number: 56\n",
"Enter Number: 25\n",
"Enter Number: 52\n",
"Enter Number: 11\n",
"Enter Number: 65\n",
"{65, 11, 45, 52, 54, 56, 25}\n"
]
}
],
"source": [
"# Input 8 numbers form user and display all unique numbers:\n",
"s1 = set()\n",
"for i in range(8):\n",
" n = int(input(\"Enter Number: \"))\n",
" s1.add(n)\n",
"print(s1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "475df313",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{18, '18'}\n"
]
}
],
"source": [
"# Can we have a set with 18 as int and 18 as a string in it.\n",
"\n",
"s2 = {18,'18'}\n",
"print(s2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "81f1743f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'20', 20} 2\n"
]
}
],
"source": [
"# Length of set after few operations:\n",
"\n",
"s3 = set()\n",
"s3.add(20)\n",
"s3.add(20.0)\n",
"s3.add('20')\n",
"print(s3,(len(s3)))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ddf0332f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Type of S:\n",
"\n",
"S = {}\n",
"type(S)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ee3faf80",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your name: Tushar \n",
"Enter your fav lang: Pyhton\n",
"Enter your name: Ayush\n",
"Enter your fav lang: C++\n",
"Enter your name: Kabir\n",
"Enter your fav lang: SQL\n",
"Enter your name: Aniket\n",
"Enter your fav lang: C\n"
]
},
{
"data": {
"text/plain": [
"{'Tushar ': 'Pyhton', 'Ayush': 'C++', 'Kabir': 'SQL', 'Aniket': 'C'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 4 frinds fav language\n",
"\n",
"favLang = {}\n",
"for i in range(4):\n",
" name = input(\"Enter your name: \")\n",
" lang = input(\"Enter your fav lang: \")\n",
" favLang[name] = lang\n",
"favLang"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "cc377b9c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your name: Tushar\n",
"Enter your fav lang: Pyhton\n",
"Enter your name: Ayush\n",
"Enter your fav lang: C++\n",
"Enter your name: Ayush\n",
"Enter your fav lang: C\n",
"Enter your name: Kabir\n",
"Enter your fav lang: SQL\n"
]
},
{
"data": {
"text/plain": [
"{'Tushar': 'Pyhton', 'Ayush': 'C', 'Kabir': 'SQL'}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Same above question but if name of 2 friends are same:\n",
"\n",
"favLang = {}\n",
"for i in range(4):\n",
" name = input(\"Enter your name: \")\n",
" lang = input(\"Enter your fav lang: \")\n",
" favLang[name] = lang\n",
"favLang"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "705aa20b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your name: Tushar\n",
"Enter your fav lang: Python\n",
"Enter your name: Ayush\n",
"Enter your fav lang: C++\n",
"Enter your name: Kabir\n",
"Enter your fav lang: C++\n",
"Enter your name: Aniket\n",
"Enter your fav lang: SQL\n"
]
},
{
"data": {
"text/plain": [
"{'Tushar': 'Python', 'Ayush': 'C++', 'Kabir': 'C++', 'Aniket': 'SQL'}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Same Q6 if lang of 2 friends are same:\n",
"\n",
"favLang = {}\n",
"for i in range(4):\n",
" name = input(\"Enter your name: \")\n",
" lang = input(\"Enter your fav lang: \")\n",
" favLang[name] = lang\n",
"favLang"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a07c70fa",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\
u001b[1;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[1;32mIn[19], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \
u001b[38;5;66;03m# Can you change the value inside the list that is contained in
the set.\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m Se \u001b[38;5;241m=\u001b[39m
{\u001b[38;5;241m8\u001b[39m,\u001b[38;5;241m7\u001b[39m,\u001b[38;5;241m12\
u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTushar\u001b[39m\
u001b[38;5;124m'\u001b[39m,[\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m5\
u001b[39m,\u001b[38;5;241m6\u001b[39m]}\n\u001b[0;32m 4\u001b[0m Se\n",
"\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"# Can you change the value inside the list that is contained in the set.\n",
"\n",
"Se = {8,7,12,'Tushar',[3,5,6]}\n",
"Se"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "725b7095",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like