Python
Python is a text-based programming language. The commands in python are
text. To make Python carry out a command you must type the command.
What is python?
Python is an interpreted, object-oriented, high-level programming language with
dynamic semantics. Its high-level built in data structures, combined with ...
Python IDLE
IDLE (Integrated Development and Learning Environment) is an integrated
development environment (IDE) for Python. The IDLE module by default. The
programming language is maintained and available from (Python Software
Foundation): https://www.python.org Here you can download the basic Python
features in one package, which includes the Python programming language
interpreter, and a basic code editor, or an integrated development environment,
called IDLE.
Top 10 uses of Python in the real world –
Web Development. ...
Data Science. ...
Artificial Intelligence and Machine Learning. ...
Enterprise Applications. ...
Education Sector. ...
Web Scraping Applications. ...
Game Development. ...
Software Development.
Naming variables in Python
The >>> symbol that you see is called a prompt.
There are rules in Python about naming variables. A variable name must:
-be one word only-no spaces
-use only letters, numbers and the underscore character
-start with a letter.
Setting the value of a variable
In Python
Variable=value
Example- password=” diamond7”
Get input and output
To get user input in python you use the command input.
The following Python command will get user input and store it as a variable
called name:
name=input(“what is your name?”)
The text inside the brackets is called the prompt. The prompt tells the user what
to input.
To produce output in Python you use the command print. After this command
you insert brackets. Whatever you put inside the brackets will be output.
e-g You might want to output:
-a number or calculation
-a string (that means some text in quote marks)
-the name of a variable.
e-g -password=”diamond7”
name=input(“What is your name?”)
print(“hello”)
print(name)
print(“your password is”,password)
Make a Python program
The shell is called the command processor.
Python shell does not save the commands. If you want to use the same
commands again, you have to type them again.
Create a file
In the Python shell, open the ‘File’ menu Choose ‘New file’. Now there are
two open windows. One is the interactive window (python shell). The other
is the editor window (file window). The file window is empty.
Important (Editor window or File window)
When you write code in a Python file, you don’t need to include the >>>
prompt. Before you run your program, you need to save it. Select File Save
from the menu and save the file as hello_world.py.
You can type code into both the interactive window and the editor window.
The difference between the two windows is in how they execute code. In
this section, you’ll learn how to execute Python code in both windows.
The Interactive window IDLE’s interactive window contains a Python
shell, which is a textual user interface used to interact with the Python
language. You can type a bit of Python code into the interactive window
and press Enter to immediately see the results.
Each command goes on a separate line.
Colors in the python program:
-Name of variables are shown in black.
-Symbols such as commas and brackets are shown in black.
-input and print function are shown in purple
-Text strings are shown in green.
python space --version
Python hello.py
Integrated Development Environment (IDE)
The software that you use to write and run programs is called an IDE. That
stands for IDE. An IDE lets you enter and save program commands. It displays
error messages. It runs the program. It lets the user enter the input and it shows
the output.
The IDE you are using to make Python programs is called IDLE. It is simple
text-based software. The program output is shown in the python shell.
Python examples
Q- print("Hello World")
Output: Hello World
Q-print('Python is powerful')
Output:
Python is powerful
Q-print('Good Morning!')
print('It is rainy today')
Output
Good Morning!
It is rainy today
Q- num = input('Enter a number ')
print('You Entered:', num)
Output
Enter a number: 10
You Entered: 10
Q-If you want to print the name of five countries, you can write:
print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")
How to Declare and use a Variable
Let see an example. We will define variable in Python and declare it as “a” and
print it.
This code is editable. Click Run to Execute
a=100
print (a)
Ans-100
We can use variables in a calculation like this:
x=3
y = 3∗ x
print(y)
Ans:9
We can implement the formula y = ax + b like this:
a=2
b=5
x=3
y = a ∗x + b
print(y)
Ans:11
Python Data Types
In computer programming, data types specify the type of data that can be stored
inside a variable. For example,
num = 24
Here, 24 (an integer) is assigned to the num variable. So the data type of num is
of the int class.
Q-This program adds two numbers
num1 = 1.5
num2 = 6.3
sum = num1 + num2
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Python String Data Type
String is a sequence of characters represented by either single or double quotes.
For example,
name = 'Python'
print(name)
message = 'Python for beginners'
print(message)
Output
Python
Python for beginners
Question-1- The villagers will dig fish ponds.
They will put water into the ponds. They will
put baby tilapia into the ponds. The villagers
need to know how big the ponds can be and
how much water they need. A farmer plans to
make a pond on his land.
He also wants to know how many fish can live
in the pond. Each cubic meter of water is
enough room for two tilapia fish.
He wants to know how much water he will
need to fill the pond.
Create a python program that takes as inputs the three
dimensions of a pond (width, length and depth). It will
output the surface area and volume of the pond and the
number of fish that can live in the pond.
Run the program and enter suitable input values.
Area=width*length
Volume=Area*depth
The number of fish=Volume*2
Width=10-meters
Length=15-meters
Depth=1.5-maters
Question-2- The villagers will use a water pipe to fill
their ponds. How many days will it takes to fill each
pond?
The input to the program is the number of liters per
second that comes out of a pipe. Work out the
number of liters per hour. Converts liters per second
to liters per hour. Converts liters per hour to liters
per day. Convert liters per day to cubic meters per
day.
Liters per second=0.5