Python
Python
Python
com
Python: Beginners
Python
Python is a popular programming language. It was created by Guido van Rossum, and
released in 1991.
It is used for:
i) web development (server-side),
ii) software development
iii) mathematics
iv) system scripting.
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.
1
Python emick.ghimire@gmail.com
2
Python emick.ghimire@gmail.com
Applications:
1. GUI-based desktop applications: Python is used to develop graphical user
interface (GUI) applications.
2. Graphic design, image processing, games, and scientific/computational
applications: Python is employed in graphics, games, and scientific computing.
3. Web frameworks and applications: Popular web frameworks
like Django and Flask are built using Python.
4. Enterprise and business applications: Python is used for various business
applications, including data analysis and automation.
5. Operating systems: Python is used in the development of operating systems and
system tools.
6. Education: Python is commonly used for teaching programming and computer
science.
7. Database access: Python provides libraries for accessing and managing databases.
8. Language development: Python is used to create and develop new programming
languages.
9. Prototyping: Python is ideal for quickly prototyping software and applications.
10. Software development: Python is used for general-purpose software
development.
11. Data science and machine learning: Python is a primary language for data
science and machine learning tasks.
12. Scripting: Python is widely used for writing scripts to automate tasks and
processes.
3
Python emick.ghimire@gmail.com
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example: #This is a comment
print("Hello, World!")
or
print("Hello, World!") #This is a comment
4
Python emick.ghimire@gmail.com
Comments can be placed at the end of a line, and Python will ignore the rest of
the line:
2. Python Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it
Example:
Var = "Geeksforgeeks"
print(Var) output: Geeksforgeeks
Rules for Python variables
• A Python variable name must start with a letter or the underscore character.
• A Python variable name cannot start with a number.
• A Python variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _).
• Variable in Python names are case-sensitive (name, Name, and NAME are three
different variables).
• The reserved words(keywords) in Python cannot be used to name the variable in
Python.
5
Python emick.ghimire@gmail.com
Python Identifier
Identifier is a name given to various programming elements such as a variable,
function, class, module or any other object.
Following are the rules to create an identifier.
1. The allowed characters are a-z, A-Z, 0-9 and underscore (_)
2. It should begin with an alphabet or underscore
3. It should not be a keyword
4. It is case sensitive
5. No blank spaces are allowed.
6. It can be of any size Valid identifiers
examples : si, rate_of_interest, student1, ageStudent Invalid identifier examples : rate of
interest, 1student, @age
Python Keywords
Keywords are the identifiers which have a specific meaning in python, there are 33
keywords in python. These may vary from version to version.
False None True and as assert break class continue def del elif else except finally for from
global if import in is lambda nonlocal not or pass raise return try while with
false true none return pass import while
and as class not else return
del assert finally in continue nonlocal
def elif if is except rise
try while for from lambda with yield
Python Numbers
Number data type stores Numerical Values. Number data type stores Numerical Values.
These are of three different types:
a) Integer & Long
b) Float/floating point
c) Complex
1.1) Integers are the whole numbers consisting of + or – sign like 100000, -99, 0, while
writing a large integer value
e.g. age=19
salary=20000
1.2 Floating Point: Numbers with fractions or decimal point are called floating point
numbers. A floating point number will consist of sign (+,-) and a decimal sign(.)
6
Python emick.ghimire@gmail.com
7
Python emick.ghimire@gmail.com
4. Sets: Set is an unordered collection of values, of any type, with no duplicate entry. Sets
are immutable.
Example
s = set ([1,2,3,4])
5. Dictionaries: Dictionaries store a key – value pairs, which are accessed using key.
Dictionary is enclosed in curly brackets.
Example
d = {1:'a', 2:'b', 3:'c'}
6. Literals: A literal is a constant that appear directly in a program and this value does
not change during the program execution i.e. remain fixed.
Example:
Num1=5
Principle_amount= 5000.00
Here 5 and 5000.00 are literals. Python support the following literals
Numeric literals
String Literals
Special Literals
Collection Literals
Operators: Operators are special symbols which represents specific operations. They are
applied on operand(s), which can be values or variables. Same operator can behave
differently on different data types.
Operators when applied on operands form an expression.
Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
8
Python emick.ghimire@gmail.com
Arithmetic operators are used with numeric values to perform common mathematical
operations:
Operator Description Syntax
+ Addition: adds two operands x+y
– Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
% Modulus: returns the remainder when the first operand is divided by the second
x%y
** Power: Returns first raised to power second x ** y
9
Python emick.ghimire@gmail.com
10
Python emick.ghimire@gmail.com
11