0% found this document useful (0 votes)
72 views8 pages

Introduction to Python Programming

this document relate to python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views8 pages

Introduction to Python Programming

this document relate to python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is Python?

-----
Python is a popular programming language. It was created by Guido van Rossum, and
released in 1991.
It is used for:

 web development (server-side),


 software development,
 mathematics,
 system scripting.

What can Python do?


 Python can be used on a server to create web applications.
 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify files.
 Python can be used to handle big data and perform complex mathematics.
 Python can be used for rapid prototyping, or for production-ready software
development.

Why Python?
 Python works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc).
 Python has a simple syntax similar to the English language.
 Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
 Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
 Python can be treated in a procedural way, an object-oriented way or a
functional way.

PYTHON BASICS---
Comments
Comments are the statements which are incorporated in the code to
give a better understanding of code statements to the user.
There are two types of comments in python.
1. Single Line 2. Multi Line
Single Line comment
A single-line comment is used to add some explanatory text in the
program for better understanding of the next line. A # sign is used to
write a single line comment in the python program For example,
# statement to add two numbers
res = 6 + 7
#Print the result
print(res)

Multiline comments
The multiline comments are written in python using triple quotes. You
can write number lines starting with triple quotes and end with triple
quotes.
For example,
'''Write a python program to display the difference between two
numbers, the first number should be larger than second number'''

n1=5 n2=2 res=n1 - n2


print(res)

Keywords (Reserved Words)


Keywords are the reserved words or pre-defined words with a special
meaning to the machine by default. So the user cannot use them
anywhere else or it cannot be changed or modified in the entire
program. They are always case-sensitive.
Identifiers
Identifiers are names used in programs to identify small units of
programs such as variables, objects, classes, functions etc. Identifiers
defined by the following few rules as follows:
1. It can be a combination of numbers and letters
2. It must start alphabets or underscore
3. Special characters are not allowed in identifiers name except
underscore
4. Spaces are not allowed in identifier names, underscore can be used to
separate two words
5. Upper Case and Lower Case letters are treated differently
Examples:
MyData, roll_no, year1 etc…

Variables---
A variable is a named location used to store data in the memory. It is helpful to think
of variables as a container that holds data which can be changed later throughout
programming.
For example, a=10 b=20

These declarations make sure that the program reserves memory for two variables
with the names a and b. The variable names stand for the memory location.

Datatypes—
In program, you have a choice to use any type of data such as real numbers,
numbers with decimals, numbers without decimals, text, etc. These type of data is
defined by datatype in Python.
Python Input and Output
We use the print() function to output data to the standard output device (screen). We
can also output data to a file. An example is given below
a = "Hello World!" print(a)

The input() function is used to accept values from the user at runtime. This function
accepts and returns the text data by default. Therefore, you need to specify the data
type if you want to use numbers or any other datatype. This process is known as
typecasting
Str = input(<String>) # Python expects the input to be of string datatype

Number = int(input(<string>)) # Input string gets converted to an integer value


before assignment

Value = float(input(<String>)) # Input string gets converted to a decimal value


before assignment.

Python Operators---
Operators are special symbols which represent computation. They are applied on operand(s),
which can be values or variables. Same operators can behave differently on different data
types. Operators when applied on operands form an expression. Operators are categorized
as Arithmetic, Relational, Logical and Assignment operators. Values and variables when
used with operators are known as operands
Conditional Statements—
Conditional statements help the machine in taking a decision according to the condition
which gets fulfilled. There exist different types of conditional statements in Python.

Simple if Statement

If you have only one condition to be execute or one possibility of output, simple if statement
is useful for the same. Simple it executes the True condition block. Suppose, If gate is open
you are allowed to go inside!
Let’s see the syntax

if <condition>:
statement(s)

Example:
gate_status = "open"
if gate_status=="open":
print("Entry is permitted")

if else statement

if <condition>:
statement(s)
else:
statement(s)

gate_status = "open"
if gate_status=="open":
print("Entry is permitted")
else:
print("You have to go back to your home, Bye - Bye")

if- elif- else ladder

Python if-elif-else ladder is used in the case when you have more than two choices or
possibilities. The elif is a short form of else if. The ladder means it will continue upto n
possiblities.

if <condition1>:
statement(s)
elif <condition2>:
statement(s)
elif <condition3>:
statement(s)
....
....
....
else:
statement(s)

Common questions

Powered by AI

Python's operators, which include arithmetic, relational, logical, and assignment operators, enable efficient handling of expressions across various data types. These operators are designed to operate seamlessly with numbers, strings, and other data types. For example, the same addition operator can add numbers or concatenate strings, enhancing Python's flexibility and reducing the need for complex, type-specific operations. This capability simplifies the development of complex expressions and algorithms in Python, making it a preferred choice for developers handling diverse data types .

Python's simple syntax allows developers to write code that is easier to read and understand, resembling the English language. This simplicity reduces the complexity involved in programming, allowing developers to write programs with fewer lines of code compared to other programming languages. The straightforward syntax, combined with Python's ability to execute code immediately due to its interpreter, makes it possible to rapidly prototype applications .

Python's suitability for rapid prototyping stems from its interpreted nature, clear syntax resembling English, and dynamic typing features. Code written in Python can be immediately executed, which speeds up the cycle of writing, testing, and refining code. These factors allow developers to quickly iterate on designs, test concepts, and refine features, significantly reducing the time from conception to implementation in the software development cycle. This speed and efficiency benefit startups or projects under tight deadlines, where rapid iteration and innovation are crucial .

Python's rules for identifiers, which include case sensitivity and restrictions on special characters and spaces, help maintain code readability and consistency. By enforcing identifiers to start with a letter or underscore and allowing only underscores as special characters, Python ensures that variable names are clean and clear. Case sensitivity distinguishes different identifiers, which encourages developers to be conscious about naming conventions. This results in code that is easier to read, understand, and maintain, facilitating collaboration and reducing errors in large codebases .

Comments in Python, which can be single-line or multiline, are vital for providing explanations and annotations within the code. They offer insights into the functionality and purpose of code sections, making it easier for others to understand and maintain. In collaborative environments, comments help team members grasp the logic and structure of the code quickly, reducing onboarding time and facilitating efficient communication among developers .

Python's input() and print() functions provide straightforward mechanisms for user interaction during runtime. The input() function allows the program to accept and process user inputs dynamically, which can then be typecast to the required data type for further operations. The print() function outputs data to the screen or an external file, enabling developers to display results or custom messages. These functions contribute to creating interactive and user-friendly applications .

Python's capability to handle big data and perform complex mathematical operations, combined with its extensive libraries like NumPy, pandas, and SciPy, makes it a powerful tool for data analysis and scientific computing. Its flexibility and performance optimization facilitate the effective management and analysis of large datasets. This enhances Python's appeal to data scientists and researchers who require efficient and scalable solutions for processing and analyzing vast amounts of data .

Python's ability to run on multiple platforms—including Windows, Mac, Linux, and Raspberry Pi—significantly enhances its usability for software development. This cross-platform compatibility allows developers to write code that can be easily deployed and executed in diverse computing environments without modification. Additionally, Python's simple syntax and interpreter system facilitate rapid development and prototyping, making it an attractive choice for developers working on different platforms .

Python's conditional statements, including if, if else, and if-elif-else, allow the program to execute logic-based decision-making processes. These statements enable the program to choose between different execution paths based on evaluated conditions. The if statement allows for simple condition checks, while the if-elif-else structure provides multiple branches for complex decision-making, allowing Python to execute different code blocks based on various conditions. This capability is crucial for creating dynamic and responsive applications .

Python's support for multiple programming paradigms, including procedural, object-oriented, and functional programming, enhances its adaptability to different programming tasks and developer preferences. This flexibility allows developers to choose the paradigm that best fits their problem-solving approach or project requirements. For instance, procedural programming is suitable for straightforward linear tasks, object-oriented programming enables modular code with reusable components, and functional programming promotes concise code with higher order functions. This adaptability makes Python versatile and applicable across various domains .

You might also like