Pip 01
Pip 01
What is Python?
Use of Python
It is used for:
● software development,
● system scripting.
● 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.
Db management
Web dev.
Cad applications
3. Object oriented
4. Interpreted
5. Dynamically typed
6. Garbage collected
7. Open source
In February 1991, the first public version of Python, version 0.9.0, was
released. This marked the official birth of Python as an open-source
project. The language was named after the British comedy series
"Monty Python's Flying Circus".
From the 1990s to the 2000s, Python gained popularity for its
simplicity, readability, and versatility. In October 2000, Python 2.0 was
released. Python 2.0 introduced list comprehensions, garbage
collection, and support for Unicode.
In December 2008, Python 3.0 was released. Python 3.0 introduced
several backward-incompatible changes to improve code readability
and maintainability.
https://www.geeksforgeeks.org/history-of-python/
https://www.tutorialspoint.com/python/python_history.htm
https://www.javatpoint.com/python-history
● Audio and Music: Python has libraries like Pyaudio, which is used
for audio processing, synthesis, and analysis, and Music21, which
is used for music analysis and generation.
Syntax:
Parameters:
○ sep - Separates the objects by a separator passed, default value = " ".
Example:
1. # Displaying a string
2. print("Hello, World!")
3.
4. # Displaying multiple values
5. name = "Aman"
6. age = 21
7. print("Name:", name, "Age:", age)
8.
9. # Printing variables and literals
10. x=5
11. y=7
12. print("x =", x, "y =", y, "Sum =", x + y)
13.
14. # Printing with formatting
15. percentage = 85.75
16. print("Score: {:.2f}%".format(percentage))
Output:
Hello, World!
X = 5 y = 7 Sum = 12
Score: 85.75%
In this example, the print statement is used to print string, integer, and float
values in a human readable format.
The print statement can be used for debugging, logging and to provide
information to the user.
Python Indentation
Example :
if 5 > 2:
Output : Error
if 5 > 2:
Example :
if 5 > 2:
Python Comments
Comments in Python are the lines in the code that are ignored by the interpreter
during the execution of the program.
Comments enhance the readability of the code and help the programmers to
understand the code very carefully. It also helps in collaborating with other
developers as adding comments makes it easier to explain the code.
Creating a Comment
#This is a comment
print("Hello, World!")
A comment does not have to be text that explains the code, it can
also be used to prevent Python from executing code:
#print("Hello, World!")
print("Cheers, Mate!")
Multiline Comments
Python does not really have a syntax for multiline comments.
Example
#This is a comment
#written in
print("Hello, World!")
Since Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your
code, and place your comment inside it:
"""
This is a comment
written in
"""
print("Hello, World!")
Just like any other language, comments in Python serve following purpose:
https://www.google.com/amp/s/www.geeksforgeeks.org/python-
comments/amp/
https://www.javatpoint.com/python-comments
Python Variables
Variables
Creating Variables
Example:
x=5
y = "John"
print(x)
print(y)
Example:
Casting
If you want to specify the data type of a variable, this can be
done with casting.
Example
Example:
x=5
y = "John"
print(type(x))
print(type(y))
Example
x = "John"
# is the same as
x = 'John'
Case-Sensitive
Variable names are case-sensitive.
Example :
a = 4
A = "Sally"
Variable Names
Example:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
2myvar = "John"
my-var = "John"
my var = "John"
There are several techniques you can use to make them more
readable:
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John"
Pascal Case
MyVariableName = "John"
Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
Example
print(x)
print(y)
print(z)
Note: Make sure the number of variables matches the number of
values, or else you will get an error.
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows
you to extract the values into variables. This is called unpacking.
Example
Unpack a list:
x, y, z = fruits
print(x)
print(y)
print(z)
Output :
Apple
Banana
Cherry
Output Variables
The Python print() function is often used to output variables.
Example
x = "Python is awesome"
print(x)
Example:
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Output :
Python is awesome
Try it Yourself »
Example
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Try it Yourself »
Notice the space character after "Python " and "is ", without them the
result would be "Pythonisawesome".
x = "Python"
y = "is"
z = "awesome"
print(x + y + z)
Output : Pythonisawesome
Example
x=5
y = 10
print(x + y)
Try it Yourself »
Example
x=5
y = "John"
print(x + y)
Try it Yourself »
Example
x=5
y = "John"
print(x, y)
Output : 5 John
Local variables in Python are the ones that are defined and
declared inside a function. We can not call this variable outside
the function.
Python3
# This function uses global variable s
def f():
s = "Welcome geeks"
print(s)
f()
Output:
Welcome geeks
Global variables in Python are the ones that are defined and
declared outside a function, and we need to use them inside a
function.
Python3
# This function has a variable with
# name same as s
def f():
print(s)
# Global scope
f()
Output:
I love Geeksforgeeks
Output :
Python Keywords
Python has a set of keywords that are reserved words that cannot
be used as variable names, function names, or any other
identifiers:
Keyword Description
as To create an alias
Or not
in To check if a value is
or A logical operator
print(False == 0)
print(True == 1)
print(None == 0)
print(None == [])
Output
True
True
False
False
https://www.geeksforgeeks.org/python-keywords/
Identifiers in Python
Identifier is a user-defined name given to a variable, function,
class, module, etc. The identifier is a combination of character
digits and an underscore. They are case-sensitive i.e., ‘num’ and
‘Num’ and ‘NUM’ are three different identifiers in python. It is a
good programming practice to give meaningful names to
identifiers to make the code understandable.
● var1
● _var1
● _1_var
● var_1
Invalid Identifiers
● !var1
● 1var
● 1_var
● var#1
● var 1
Python Datatypes
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
You can get the data type of any object by using the type()
function:
x=5
print(type(x))
https://www.w3schools.com/python/python_numbers.asp
https://www.geeksforgeeks.org/python-data-types/
In Python, the bytes datatype is used to handle binary data. It's essentially a
sequence of bytes, which are 8-bit numbers ranging from 0 to 255. Here’s a basic
example of how to use the bytes datatypeThe b prefix in byte_data = b'hello world'
is used to denote that the string is a bytes literal
Python Operators
https://www.geeksforgeeks.org/python-operators/
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Identity operators
● Membership operators
● Bitwise operators
Arithmetic Operators in Python
Python Arithmetic operators are used to perform basic mathematical
operations like addition, subtraction, multiplication, and division.
In Python 3.x the result of division is a floating-point while in Python 2.x
division of 2 integers was an integer. To obtain an integer result in Python 3.x
floored (// integer) is used.
Subtraction: subtracts
– x–y
two operands
Multiplication:
* multiplies two x*y
operands