{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"introduction.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"U3cJsdW5YveN","colab_type":"text"},"source":["# Introduction\n","\n","\n","\n"]},{"cell_type":"markdown","metadata":{"id":"xbBRXYdOc_g0","colab_type":"text"},"source":["## [Python Program to Print Hello world!](https://www.programiz.com/python-programming/examples/hello-world)\n","A simple program that displays “Hello, World!”. It's often used to illustrate the syntax of the language."]},{"cell_type":"code","metadata":{"id":"g4i2QvqBY1p6","colab_type":"code","colab":{}},"source":["# This program prints Hello, world!\n","\n","print('Hello, world!')"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"4FojPwohY3j6","colab_type":"text"},"source":["In this program, we have used the built-in print() function to print the string Hello, world! on our screen.\n","\n","[String](https://www.programiz.com/python-programming/string) is a sequence of characters. In Python, strings are enclosed inside single quotes, double quotes or triple quotes (''', \"\"\")."]},{"cell_type":"markdown","metadata":{"id":"U4EGjo4Nb1Ti","colab_type":"text"},"source":["\n","\n","---\n","\n","## [Python Program to Add Two Numbers](https://www.programiz.com/python-programming/examples/add-number)\n","In this program, you will learn to add two numbers and display it using print() function.\n","n the program below, we've used the arithmetic addition operator (+) to add two numbers.\n","\n","### Source Code"]},{"cell_type":"code","metadata":{"id":"DEsO0lZab5Fn","colab_type":"code","colab":{}},"source":["# This program adds two numbers\n","num1 = 1.5\n","num2 = 6.3\n","# Add two numbers\n","sum = float(num1) + float(num2)\n","# Display the sum\n","print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"TyyZ1wF6ZTfx","colab_type":"text"},"source":["Changing this operator, we can subtract (-), multiply (*), divide (/), floor divide (//) or find the remainder (%) of two numbers.\n","### Source Code: Add Two Numbers Provided by The User"]},{"cell_type":"code","metadata":{"id":"2vsD_PkrZZKE","colab_type":"code","colab":{}},"source":["# Store input numbers\n","num1 = input('Enter first number: ')\n","num2 = input('Enter second number: ')\n","# Add two numbers\n","sum = float(num1) + float(num2)\n","# Display the sum\n","print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"_nTZN0xJZq1W","colab_type":"text"},"source":["\n","\n","---\n","\n","\n","## [Python Program to Find the Square Root](https://www.programiz.com/python-programming/examples/square-root)\n","In this program, you'll learn to find the square root of a number using exponent operator and cmath module.\n","### Source Code: For positive numbers using exponent **"]},{"cell_type":"code","metadata":{"id":"1AdbkpPRZxSa","colab_type":"code","colab":{}},"source":["# Python Program to calculate the square root\n","\n","# Note: change this value for a different result\n","num = 8 \n","\n","# uncomment to take the input from the user\n","#num = float(input('Enter a number: '))\n","num_sqrt = num ** 0.5\n","print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Hg9EsVOMZzgA","colab_type":"text"},"source":["In this program, we store the number in num and find the square root using the ** exponent operator. This program works for all positive real numbers. But for negative or complex numbers, it can be done as follows.\n","\n","### Source code: For real or complex numbers using cmath module"]},{"cell_type":"code","metadata":{"id":"N8jK3tRIZ1Xx","colab_type":"code","colab":{}},"source":["# Find square root of real or complex numbers\n","# Import the complex math module\n","import cmath\n","\n","# change this value for a different result\n","num = 1+2j\n","\n","# uncommment to take input from the user\n","#num = eval(input('Enter a number: '))\n","num_sqrt = cmath.sqrt(num)\n","print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"eyRmo_K6Z285","colab_type":"text"},"source":["In this program, we use the sqrt() function in the cmath (complex math) module.\n","\n","Notice that we have used the eval() function instead of float() to convert complex number as well. Also notice the way in which the output is formatted.\n","\n","Look here for more about [string formatting in Python](http://docs.python.org/3/library/string.html#format-string-syntax)."]},{"cell_type":"markdown","metadata":{"id":"sAbJoE6XZaot","colab_type":"text"},"source":["In this program, we asked the user to enter two numbers and this program displays the sum of two numbers entered by user.\n","\n","We use the built-in function input() to take the input. input() returns a string, so we convert it into number using the float() function.\n","\n","Alternative to this, we can perform this addition in a single statement without using any variables as follows."]},{"cell_type":"code","metadata":{"id":"zAER7_AEZcvK","colab_type":"code","colab":{}},"source":["print('The sum is %.1f' %(float(input('Enter first number: '))+float(input('Enter second number: '))))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"W7mLHveYZeD_","colab_type":"text"},"source":["Although this program uses no variable (memory efficient), it is not quite readable. Some people will have difficulty understanding it. It is better to write clear codes. So, there is always a compromise between clarity and efficiency. We need to strike a balance."]},{"cell_type":"markdown","metadata":{"id":"w7FawKZJaAL9","colab_type":"text"},"source":["\n","\n","---\n","\n","\n","## [Python Program to Calculate the Area of a Triangle](https://www.programiz.com/python-programming/examples/area-triangle)\n","In this program, you'll learn to calculate the area of a triangle and display it.\n","\n","If a, b and c are three sides of a triangle. Then,\n","\n","```\n","s = (a+b+c)/2\n","area = √(s(s-a)*(s-b)*(s-c))\n","```\n","### Source Code\n"]},{"cell_type":"code","metadata":{"id":"-e3pi_6-aG94","colab_type":"code","colab":{}},"source":["# Python Program to find the area of triangle\n","\n","a = 5\n","b = 6\n","c = 7\n","\n","# Uncomment below to take inputs from the user\n","# a = float(input('Enter first side: '))\n","# b = float(input('Enter second side: '))\n","# c = float(input('Enter third side: '))\n","\n","# calculate the semi-perimeter\n","s = (a + b + c) / 2\n","\n","# calculate the area\n","area = (s*(s-a)*(s-b)*(s-c)) ** 0.5\n","print('The area of the triangle is %0.2f' %area)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"R3jsBMUpaJ74","colab_type":"text"},"source":["In this program, area of the triangle is calculated when three sides are given using [Heron's formula](http://en.wikipedia.org/wiki/Heron%27s_formula).\n","\n","If you need to calculate area of a triangle depending upon the input from the user, input() function [link text](https://www.programiz.com/python-programming/methods/built-in/input) can be used."]},{"cell_type":"markdown","metadata":{"id":"d1M82ny2aYqi","colab_type":"text"},"source":["\n","\n","---\n","\n","\n","## [Python Program to Solve Quadratic Equation](https://www.programiz.com/python-programming/examples/quadratic-roots)\n","This program computes roots of a quadratic equation when coefficients a, b and c are known.\n","\n","The standard form of a quadratic equation is:\n","\n","```\n","ax2 + bx + c = 0, where\n","a, b and c are real numbers and\n","a ≠ 0\n","```\n","\n"]},{"cell_type":"code","metadata":{"id":"DcFl2S62admy","colab_type":"code","colab":{}},"source":["# Solve the quadratic equation ax**2 + bx + c = 0\n","\n","# import complex math module\n","import cmath\n","\n","a = 1\n","b = 5\n","c = 6\n","\n","# To take coefficient input from the users\n","# a = float(input('Enter a: '))\n","# b = float(input('Enter b: '))\n","# c = float(input('Enter c: '))\n","\n","# calculate the discriminant\n","d = (b**2) - (4*a*c)\n","\n","# find two solutions\n","sol1 = (-b-cmath.sqrt(d))/(2*a)\n","sol2 = (-b+cmath.sqrt(d))/(2*a)\n","\n","print('The solution are {0} and {1}'.format(sol1,sol2))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"FzRnO-7Kafro","colab_type":"text"},"source":["We have imported the cmath module to perform complex square root. First we calculate the discriminant and then find the two solutions of the quadratic equation.\n","\n","You can change the value of a, b and c in the above program and test this program."]},{"cell_type":"markdown","metadata":{"id":"BqejtZ0hatwU","colab_type":"text"},"source":["## [Python Program to Swap Two Variables](https://www.programiz.com/python-programming/examples/swap-variables)\n","In this example, you will learn to swap two variables by using a temporary variable and, without using temporary variable.\n","### Source Code: Using temporary variable"]},{"cell_type":"code","metadata":{"id":"Z0wgil8NayAO","colab_type":"code","colab":{}},"source":["# Python program to swap two variables\n","\n","# To take input from the user\n","# x = input('Enter value of x: ')\n","# y = input('Enter value of y: ')\n","\n","x = 5\n","y = 10\n","\n","# create a temporary variable and swap the values\n","temp = x\n","x = y\n","y = temp\n","\n","print('The value of x after swapping: {}'.format(x))\n","print('The value of y after swapping: {}'.format(y))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"JTCHwS4pazZf","colab_type":"text"},"source":["In this program, we use the temp variable to temporarily hold the value of x. We then put the value of y in x and later temp in y. In this way, the values get exchanged.\n","\n","### Source Code: Without Using Temporary Variable\n","In python programming, there is a simple construct to swap variables. The following code does the same as above but without the use of any temporary variable.\n","\n","\n","```\n","x,y = y,x\n","```\n","If the variables are both numbers, we can use arithmetic operations to do the same. It might not look intuitive at the first sight. But if you think about it, its pretty easy to figure it out.Here are a few example\n","\n","Addition and Subtraction\n","\n","```\n","x = x + y\n","y = x - y\n","x = x - y\n","```\n","Multiplication and Division\n","\n","```\n","x = x * y\n","y = x / y\n","x = x / y\n","```\n","\n","XOR swap\n","\n","This algorithm works for integers only\n","\n","```\n","x = x ^ y\n","y = x ^ y\n","x = x ^ y\n","```\n","\n","\n","\n"]}]}