Python
Python is an interpreted , interactive, object oriented ,beginner
level ,high level programming language. There is a huge collection of
libraries available in [Link] is developed in [Link] of python
is Guido van Rossum.
What can Python do?
• Python can be used on a server to create web applications.
• Python can connect to database systems. It can also read
and modify files.
• Python can be used for web development , software
development, game development etc
Why Python?
• Python works on different platforms (Windows, Mac,
Linux 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.
• Python syntax
The Python print() function is often used to output variables. E
g: print (“hello world”)
create the program on vs code and save that file as .py
file.
For example, [Link]
In the print() function, you output multiple variables, separated
by a comma: E g: x = "Python" y = "is" z = "awesome"
print(x, y, z)
You can also use the + operator to output multiple variables:
E g : x = "Python " y = "is "
z = "awesome" print(x
+ y + z)
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code
is for readability only, the indentation in Python is very
important.
Python uses indentation to indicate a block of code.
• Variables Python
In Python,
Variables are containers for storing data values variables
are created when you assign a value to it:
Rules for Python variables:
• A variable name must start with a letter or the underscore
character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are
three different variables)
• A variable name cannot be any of the Python keywords.
.Python Data Types
Variables can store data of different types, and different types
can do different things.
There are mainly 5 types of data types are available in python:
Text types-str
Numeric types-int, float
Sequence types-list, tuple, range
Mapping types- dict
Set types-set
[Link] type-string(str)
String is a collection of characters. String is defined by using
str keyword . string is defined in’ ’ or ” ” .
E g: g= “hello world”
Print(g)
[Link] types-integer(int),floating point(float)
Integer is used to store a whole number without
decimals
E g: a=6
Print(a)
Float is used to store positive or negative numbers
with a decimal point.
E g : a =3.14
Print(a)
.Python Variables - Assign single
values and Multiple Values
Asssiging of single values E
g: x = 5 y = "Hello,
World!" print(x)
print(y) assigning of
multiple values
E g : x ,y ,z=”apple”,”mango”,4
Print(x)
Print(y)
Print(z)
Assigning one value to multiple variables
E g: x = y = z = "Orange" print(x)
print(y) print(z)
• Get the Type
you can get the data type of a variable with the type() function.
E g:x= 5y
= "John"
print(type(x))
print(type(y))
• Comments
Python has commenting capability for the purpose of in-code
documentation.
In Python, there are two types of comments
1. Single-line Comments
2. Multi-line Comments
1. Single-line Comments-In Python, single-line comments are
used for commenting out single lines of code. These comments
start with a # symbol. The symbol is then followed by the text
explaining the line of code.
# Python program for defining a single-line comment
print('Hello World!')
[Link]-line Comments-If you want to write a comment that
spans multiple lines, multiline comments can be used. These
comments may be used to describe a particular code block.
Multiline comments can be declared using two ways. You can
specify these comments using ''' (Triple single quotes) or
""" (Triple double quotes). The quotes are mentioned at
the beginning and at the end of the comments.
"""
This is a multi-line comment
written in
more than just one line
"""
'''
This is another multi-line comment
written in
more than just one line
'''
print("Hello, World!")
• Single or Double Quotes?
String variables can be declared either by using single or
double quotes: E g: x = "John" print(x) x = 'John'
print(x)
.Case-Sensitive
Python is a case−sensitive programming language. This means
that it considers uppercase and lowercase letters differently.
E g: a = 4 A
= "Sally"
print(a)
print(A)
-=
.Type Conversion
You can convert from one type to another data type.
Eg:
x = 1 # int y
= 2.8 # float
#convert from int to float: a
= float(x)
#convert from float to int: b
= int(y)
print(a) print(b)
print(type(a))
print(type(b))
Python operators
Operators are used to perform operations on variables and
values.
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
+ addition , - subtraction , * multiplication , / division ,** exponentiation , // floor division
• Python Assignment Operators
Assignment operators are used to assign values to variables:
= equal to ,+= addition equal to,-= subtraction equal to, *= multiplication equal to, /= division
equal to, **= exponentiation equal to, //= floor division equal to
• Python comparison operators
Comparison operators are used to compare two
values:
== equal , != not equal , < less than , > greater than , <= less
than or equal to , >= greater than or equal t0
• Python Logical Operators
Logical operators are used to combine conditional statements:
And=returns true if both statements are true
Or=returns true if one of the statements is true
Not=returns false, if the result is true