{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Q1 : Write a python program to print the square of all numbers from 0 to 10." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "for i in range(11):\n", " print(f\"Square of {i} = {i**2}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q2 : Suppose we have a list of integers\n", "> `numbers = [1, 2, 3, 4, 5, 6]`\n", "- How to add 5 to each number?\n", "- What if we want to add 5 to only the second to the fifth number?\n", "- What if we want to add 5 to numbers with only even-numbers?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "numbers = [1, 2, 3, 4, 5, 6]\n", "\n", "# for i in range(len(numbers)):\n", "# numbers[i]+=5\n", "# print(numbers)\n", "\n", "# for i in range(2,len(numbers),3):\n", "# numbers[i] += 5\n", "# print(numbers)\n", "\n", "# for i in range(1, len(numbers), 2):\n", "# numbers[i] += 5\n", "# print(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q3 : Write a Python program that accepts a word from the user and reverse it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "word = input(\"Input a word to reverse: \")\n", "\n", "for char in range(len(word) - 1, -1, -1):\n", " print(word[char], end=\"\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q : Write a program to reverse a given integer number." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = int(input(\"Input an integer to reverse: \"))\n", "reverse_number = 0\n", "print(\"Given Number \", num)\n", "while num > 0:\n", " remainder = num % 10\n", " reverse_number = (reverse_number * 10) + remainder\n", " num = num // 10\n", "print(\"Revered Number \", reverse_number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q4 : There are two lists - first contains students' names and second list contains their corresponding marks.\n", "> `students = [\"Kevin\", \"Zara\", \"Tom\", \"Chris\", \"Pritam\", \"Mike\"]`\n", "\n", "> `scores = [80, 76, 92, 55, 81, 72]`\n", "- Print each student's name and his/her corresponding marks in each line." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "students = [\"Kevin\", \"Zara\", \"Tom\", \"Chris\", \"Pritam\", \"Mike\"]\n", "scores = [80, 76, 92, 55, 81, 72]\n", "\n", "for i in zip(students, scores):\n", " print(f\"{i[0]} has scored {i[1]}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q5 : Use a for loop to display elements from a given list which are present at even positions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n", "for i in my_list[1::2]:\n", " print(i, end=\" \")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q6: Write a Python program which takes two digits m (row) and n (column) as input and generates a two-dimensional array. The element value in the i-th row and j-th column of the array should be `i*j`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "row_num = int(input(\"Input number of rows: \"))\n", "col_num = int(input(\"Input number of columns: \"))\n", "multi_list = [[0 for col in range(col_num)] for row in range(row_num)]\n", "\n", "for row in range(row_num):\n", " for col in range(col_num):\n", " multi_list[row][col]= row*col\n", "\n", "multi_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q7 : Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number's digits." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "number = input(\"Enter an integer number: \")\n", "count=0\n", "for num in number:\n", " n = int(num)\n", " count += n\n", "\n", "print(count)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q8 : Write an interactive program that takes a number from user to calculate factorial of that number.ΒΆ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# n! = n * (n - 1) * (n - 2) * .......* 3 * 2 * 1\n", "# 0! = 1; 1! = 1\n", "\n", "### START CODE HERE ###\n", "\n", "number = int(input(\"Enter an integer number: \"))\n", "\n", "factorial = 1\n", "if number < 0:\n", " print(\"Please provide a positive integer.\")\n", "elif number <= 1:\n", " print(f\"Factorial of {number} = 1.\")\n", "else:\n", " for i in range(2, number+1):\n", " factorial *= i\n", " print(f\"Factorial of {number} = {factorial}.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using a while loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using recursive function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q9 : Write a program that takes an integer n as argument and return the n-th Fibonacci term. If n is negative then the function should return nothing.\n", "\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### START CODE HERE ###\n", "\n", "number = int(input(\"Enter an integer number: \"))\n", "\n", "def fib(n):\n", " first, second = 0, 1\n", " if n == 0:\n", " return 0\n", " for i in range(n-1):\n", " first, second = second, first + second\n", " return second\n", "\n", "fib(number)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using recursive function" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }