1. What is Python?
- Python is a high-level programming language, with applications in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence.
- It is very popular and used by organizations such as Google, NASA, the CIA, and Disney.
- Python is processed at runtime by the interpreter. There is no need to compile your program before executing it.
- The three major versions of Python are 1.x, 2.x and 3.x. These are subdivided into minor versions, such as 2.7 and 3.3.
- Python has several different implementations, written in various languages. CPython, is the most popular by far.
Ques 1. Python is a:
Ques 2. Which language is the implementation of python?
2. Python Console-
- In this topic, we will start using Python, via the Python console. The first step is to download Python from www.python.org.
- You should download the Python 3.x version that is compatible with your operating system. Once installed, the Python console can be accessed using one of several ways, including using the command line, running the Python interpreter directly, or running a GUI that comes installed with Python called IDLE.
- After running IDLE, you should see a prompt of three right arrows. Type in "print('Hello world!')" and press enter. You should see the following: >>> print('Hello world!')
Hello world! - The Python console is a program that allows you to enter one line of Python code, repeatedly executes that line, and displays the output. This is known as a REPL – a read-eval-print loop.
After the "print('Hello world!')" line's output has been displayed, you can re-enter the code, and you will get the same output again. Try entering the same code, but with different text:
>>> print('Hello world!')
Hello world!
>>> print('Hello world!')
Hello world!
>>> print('Spam and eggs...')
Spam and eggs...
- When finished with the Python console, you will want to close it. You can close IDLE in the same way you close other programs on your operating system, but other kinds of consoles must be closed by different methods. To close a console, type in "quit()" or "exit()", after wrtiing your program and press enter.
Ques1. How to print "Hi"?
Ques2. How to print "ni ni ni"?
Ques3. What function closes python console?
3. Simple Operation
- Python has the capability of carrying out calculations. Enter a calculation directly into the Python console, and it will output the answer.
>>> 2 + 2
4
>>> 5 + 4 - 3
6
- Python also carries out multiplication and division, using an asterisk to indicate multiplication and a forward slash to indicate division.
>>> 2 * (3 + 4)
14
>>> 10 / 2
5.0
- In python we follow the rule pf PEMDAS as like BODMAS in maths. Parentheses Exponential Multiplication Division Addition Subtraction.
- Using a single slash to divide numbers produces a decimal (or float, as it is called in programming). We'll have more about floats in a later topic.
- The minus sign indicates a negative number. Operations are performed on negative numbers, just as they are on positive ones. The plus signs can also be put in front of numbers, but this has no effect, and is mostly used to emphasize that a number is positive to increase readability of code.
>>> -7
-7
>>> (-7 + 2) * (-4)
20
- Dividing by zero in Python produces an error, as no answer can be calculated. In Python, the last line of an error message indicates the error's type. Read error messages carefully, as they often tell you how to fix a program!
>>> 11 / 0
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: division by zero
Ques1. What does this code output?
>>> 1 + 2 + 3
Ques2. What is output by this code?
>>> (4 + 8) / 2
Ques3. Fill in the blank to make this code correct.
>>> ( _ 5 - 1) * 3
-18
Ques4. What is the output of this code?
>>>(17+94)/(-5+5)
4. Floats
- Floats are used in Python to represent numbers that aren't integers. Some examples of numbers that are represented as floats are 0.5 and -7.8237591.
- They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. Extra zeros at the number's end are ignored.
>>> 3/4
0.75
>>> 9.8765000
9.8765
- Computers can't store floats perfectly accurately, in the same way that we can't write down the complete decimal expansion of 1/3 (0.3333333333333333...). Keep this in mind, because it often leads to infuriating bugs!
- As you saw previously, dividing any two integers produces a float. A float is also produced by running an operation on two floats, or on a float and an integer.
>>> 8 / 2
4.0
>>> 6 * 7.0
42.0
>>> 4 + 1.65
5.65
- A float can be added to an integer, because Python silently converts the integer to a float. However, this implicit conversion is the exception rather the rule in Python - usually you have to convert values manually if you want to operate on them.
Ques1. Which of these will not be stored as a float?
a) 7 b) 7.0 c) 2/4
Ques2. Output of this code.
>>> 1 + 2 + 3 + 4.0 + 5