Numbers in Python Programming | Calculator in Python | Data Types in Python | Python as a Calculator
Numbers In Python, numbers are a fundamental data type used to represent numeric values. There are primarily three types of numbers in Python: Integers (int): These are whole numbers without decimal points. They can be positive or negative. print(2023) print(-15) Floating-Point Numbers (float): These are numbers with decimal points or in scientific notation. print(3.14) print(1.23e-3) Complex Numbers (complex): These are numbers in the form a + bj, where a and b are real numbers, and j represents the imaginary unit. print(2+3j) Python supports various arithmetic operations, including addition (+), subtraction (-), multiplication (*), division (/), floor division (//), and modulus (%). For more advanced operations, you might need to use built-in functions or libraries. It's important to remember that due to floating-point precision, calculations involving floating-point numbers might not always yield exact results. Additionally, when working with complex numbers, you can use the built-in functions like real and imag to access the real and imaginary parts of a complex number. Keep in mind that Python's dynamic typing allows you to mix different numeric types in calculations, but the result type will depend on the operation and the operands involved. print(2+2) print(50-5*6) print(50-5*6/4) print((50-5*6)/4) print(8/5) The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float. We will see more about numeric types later in the tutorial. Division (/) always returns a float. To do floor division and get an integer result you can use the // operator; to calculate the remainder you can use %: print(17/3) print(17//3) print(17%3) print(5*3+2) How to Get Started With Python? Python is a cross-platform programming language, which means that it can run on multiple platforms like Windows, macOS, Linux, and has even been ported to the Java and .NET virtual machines. It is free and open-source. Even though most of today's Linux and Mac have Python pre-installed in it, the version might be out-of-date. So, it is always a good idea to install the most current version. The Easiest Way to Run Python The easiest way to run Python is by using PyCharm. Install Python Separately If you don't want to use Thonny, here's how you can install and run Python on your computer. Download the latest version of Python. Run the installer file and follow the steps to install Python During the install process, check Add Python to environment variables. This will add Python to environment variables, and you can run Python from any part of the computer. Also, you can choose the path where Python is installed. 1. Run Python in Immediate mode Once Python is installed, typing python in the command line will invoke the interpreter in immediate mode. We can directly type in Python code, and press Enter to get the output. Try typing in 1 + 1 and press enter. We get 2 as the output. This prompt can be used as a calculator. To exit this mode, type quit() and press enter. 2. Run Python in the Integrated Development Environment (IDE) We can use any text editing software to write a Python script file. We just need to save it with the .py extension. But using an IDE can make our life a lot easier. IDE is a piece of software that provides useful features like code hinting, syntax highlighting and checking, file explorers, etc. to the programmer for application development. By the way, when you install Python, an IDE named IDLE is also installed. You can use it to run Python on your computer. It's a decent IDE for beginners. When you open IDLE, an interactive Python Shell is opened. Now you can create a new file and save it with .py extension. For example, hello.py Your first Python Program Now that we have Python up and running, we can write our first Python program. In the following examples, input and output are distinguished by the presence or absence of prompts and to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter. Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command. You can toggle the display of prompts and output by clicking on in the upper-right corner of an example box. If you hide the prompts and output for an example, then you can easily copy and paste the input lines into your interpreter.
Download
0 formatsNo download links available.