100% found this document useful (1 vote)
18 views

PYTHON PROGRAMMING

Uploaded by

kaylawendo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
18 views

PYTHON PROGRAMMING

Uploaded by

kaylawendo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON PROGRAMMING

➢ Python is a high-level programming language which is open source (any program


whose source code is made available for use or modification by users or other developers)
and object oriented.
➢ Python is an interactive language with simple syntax (the rules that control the structure
of the symbols, punctuation, and words of a programming language).
➢ Python is an interpreter-based scripting language which can be used to:
▪ Create web-based applications.
▪ Handle large amounts of data.
▪ Perform complex calculations.
▪ Connect to database systems and read & modify files.
Features of Python
1. Source code is freely available.
2. Uses simple English-like structure and easy to learn.
3. It supports Graphical User Interface (GUI).

Writing and executing Commands in Python


To write and run Python programs use Python IDLE (Integrated Development Learning
Environment) that comes with Python. It allows you to edit, run and debug a python program
from a single interface.
To launch Python IDLE, click START then click on its icon IDLE (Python 3.11 64-bit) or
any other version installed

This always starts up in the Python Shell.

1 of 4
In the Python Shell
1. Select File then New File to open the Python script editor.
2. Write the statements to be executed.
3. Save the file by selecting File then Save AS option.
4. To execute the code select Run the Run Module or press F5.
5. The output will be visible in the previously open Python Shell.

Rules of programming in Python


1. Python is case-sensitive, which means, for example, Name and name have different
meanings.
2. Use English names in programming.
3. Python is sensitive to indentations, that marks a block segment in your code.
4. A hashtag # creates a comment in the code. These are ignored by the compiler.

Keywords/commands in Python
input is used to fetch an input from the user.
print is used to output information on the screen.

VARIABLE IN PROGRAMMING
A variable in a computer program is a named data store than contains a value that may
change during the execution of a program.
A constant in a computer program is a named data store than contains a value that does not
change during the execution of a program.
Variables and constants should be given meaningful names.
Using an undefined variable in a statement causes an error called Name Error.

Rules for naming variables in Python


1. A variable name can consist of letters, digits and underscore (_).
2. A variable name can start with a letter or an underscore but not with a digit.
3. A variable name cannot have space in it.

2 of 4
4. Keywords are not allowed as a variable name eg def, while, for, if etc.
5. Python is case-sensitive, variables in upper case are different to those in lower case.

Declaring variables
Every variable has a data type which defines the format of the variable.
For example x=3.
It means value 3 will be assigned to the integer variable x automatically.

Assigning multiple values to multiple variables


Multiple variables can be assigned values using a single assignment statement.
For example x, y, z =15, 18.5, “Hello”
It means vale 15 will be assigned to variable x, 18.5 to y and “Hello” to z.

Assigning the same value to multiple variables


For example x = y = z =25
The above code assigns the same value 25 to the 3 variables x, y and z.

Using input( ) function for user input


To fetch an input from the user, the input ( ) function is used.
Syntax
input (<prompt>)

How the input ( ) function works:


1. When the input ( ) function is executed, the program flow stops until the user submits an
input value.
2. The prompt is displayed on the output screen to ask a user to enter an input value.
3. Whatever value is entered by the user as an input, the input ( ) function converts it into a
string. If any numeric value is entered, it converts into a string.
4. To treat input as numeric value, an explicit conversion int for integers or float for real
numbers is needed.

3 of 4
Example 1

Output

Example 2
If you do not use int or float function, the + operator will concatenate two strings.

Output

Data types used in Python.


Data type Description
str String
int Integer number
float / eval Real number
bool Boolean

4 of 4

You might also like