0% found this document useful (0 votes)
2 views18 pages

Python

Python is a high-level, interpreted programming language that is easy to read and write, making it suitable for various applications such as web development, AI, and data science. It features a large community and extensive libraries, and its syntax emphasizes readability through indentation. The document also covers Python's data types, operators, and provides a roadmap for learning Python over 15 days.

Uploaded by

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

Python

Python is a high-level, interpreted programming language that is easy to read and write, making it suitable for various applications such as web development, AI, and data science. It features a large community and extensive libraries, and its syntax emphasizes readability through indentation. The document also covers Python's data types, operators, and provides a roadmap for learning Python over 15 days.

Uploaded by

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

Python

What is Python?

Python is a high-level, interpreted programming language developed by Guido van Rossum


in 1991.

 High-level: Easy to read and write, closer to human language than machine code.
 Interpreted: Python code runs line by line, so you don’t need to compile it like C or
Java.
 General-purpose: Can be used for many types of programming (web, AI, data science,
etc.).
 Open-source: Free to use and has a huge community.

Why do we use Python?


1. Easy to Learn & Read
o Python’s syntax is simple and clean.
2. Versatile / Multi-purpose
o Web development (Django, Flask)
o Data analysis & visualization (Pandas, Matplotlib)
o Machine Learning & AI (TensorFlow, PyTorch, Scikit-learn)
o Automation & scripting
o Game development, IoT, and more
3. Large Community & Libraries
o Thousands of ready-to-use libraries for AI, data, web, math, etc.
o Community support makes problem-solving easier.
4. Cross-Platform
o Python runs on Windows, Linux, Mac, and even mobile devices.
5. Good for Beginners & Professionals
o Easy to start for beginners but also powerful for advanced projects.

Python Syntax:

1. Python syntax can be executed by writing directly in the Command Line:

>>> print("Hello, World!")


Hello, World!

2. Or by creating a python file on the server, using the .py file extension, and running it in
the Command Line:
C:\Users\Your Name>python [Link]
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.

1)Python uses indentation to indicate a block of code.

Example
if 5 > 2:
print("Five is greater than two!")

2)Python will give you an error if you skip the indentation:

Example
if 5 > 2:
print("Five is greater than two!")

3)The number of spaces is up to you as a programmer, the most common use is four, but it has to
be at least one.

Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")

4)You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:

Example
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")

Comments
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")

Multiline Comments

Python does not really have a syntax for multiline comments.

To add a multiline comment you could insert a # for each line:

Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")

you can use a multiline string(Triple quotes).

Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")

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
x=5
y = "John"
print(x) #5
print(y) #John
Variables do not need to be declared with any particular type, and can even change type after
they have been set.

Example
x=4 # x is of type int
x = "Sally" # x is now of type str
print(x) #Sally

Casting

If you want to specify the data type of a variable, this can be done with casting.

Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

Get the Type

You can get the data type of a variable with the type() function.

Example
x=5
y = "John"
print(type(x)) #int
print(type(y)) #String

Single or Double Quotes

String variables can be declared either by using single or double quotes:

Example
x = "John"
# is the same as
x = 'John'

Case-Sensitive

Variable names are case-sensitive.

Example

This will create two variables:


a=4
A = "Sally"
#A will not overwrite a

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).

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.

Assign Multiple Values


Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line:

Example
x, y, z = "Orange", "Banana", "Cherry"
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.

One Value to Multiple Variables

And you can assign the same value to multiple variables in one line:

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:

fruits = ["apple", "banana", "cherry"]


x, y, z = fruits
print(x)
print(y)
print(z)

In the print() function, when you try to combine a string and a number with the + operator,
Python will give you an error:

Example
x=5
y = "John"
print(x + y)
#TypeError: unsupported operand type(s) for +: 'int' and 'str'

Global Variables
 Variables that are created outside of a function are known as global variables.
 Global variables can be used by everyone, both inside of functions and outside.
Example
x = "awesome"
def myfunc():
print("Python is " + x) # Python is awesome
myfunc()

If you create a variable with the same name inside a function, this variable will be local, and can
only be used inside the function. The global variable with the same name will remain as it was,
global and with the original value.

Example

x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x) #Python is fantastic
myfunc()
print("Python is " + x) #Python is awesome

The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be
used inside that function.

To create a global variable inside a function, you can use the global keyword.

Example

If you use the global keyword, the variable belongs to the global scope:

def myfunc():
global x
x = "fantastic"

myfunc()

print("Python is " + x) #Python is fantastic

Use the global keyword if you want to change a global variable inside a function.

Example

To change the value of a global variable inside a function, refer to the variable by using
the global keyword:

x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x) #Python is fantastic

Built-in Data Types

Python has the following data types built-in by default, in these categories:
Text Type: str

Numeric Types: int, float, complex


Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType

There are three numeric types in Python:

 int
 float
 complex

Variables of numeric types are created when you assign a value to them:

Example

x = 1 # int
y = 2.8 # float
z = 1j # complex

Float can also be scientific numbers with an "e" to indicate the power of 10.

x = 35e3

Complex numbers are written with a "j" as the imaginary part.

Type Conversion

You can convert from one type to another with the int(), float(), and complex() methods:

Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:
a = float(x)

#convert from float to int:


b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))

Note: You cannot convert complex numbers into another number type.

Random Number

Python does not have a random() function to make a random number, but Python has a built-in
module called random that can be used to make random numbers:

Example

Import the random module, and display a random number from 1 to 9:

import random

print([Link](1, 10))

Python Operators
Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y

Division in Python

Python has two division operators:

 / - Division (returns a float)


 // - Floor division (returns an integer)

Assignment Operators
Assignment operators are used to assign values to variables:

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

//= x //= 3 x = x // 3

**= x **= 3 x = x ** 3

&= x &= 3 x=x&3


|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

:= print(x := 3) x=3
print(x)

The Walrus Operator

Python 3.8 introduced the := operator, known as the "walrus operator". It assigns values to
variables as part of a larger expression:

Example
numbers = [1, 2, 3, 4, 5]
count = len(numbers)
if count > 3:
print(f"List has {count} elements")

if (count := len(numbers)) > 3:


print(f"List has {count} elements")

Comparison Operators
Comparison operators are used to compare two values. Comparison operators
return True or False based on the comparison.

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

Chaining Comparison Operators

Python allows you to chain comparison operators:

Example
x=5
print(1 < x < 10)

print(1 < x and x < 10)

Logical Operators

Logical operators are used to combine conditional statements

Operator Description Example

and Returns True if both statements are x < 5 and x < 10


true

or Returns True if one of the statements x < 5 or x < 4


is true

not Reverse the result, returns False if the not(x < 5 and x < 10)
result is true

Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually
the same object, with the same memory location:

Operator Description Example

is Returns True if both variables are the same object x is y

is not Returns True if both variables are not the same object x is not y
Difference Between is and ==

 is - Checks if both variables point to the same object in memory


 == - Checks if the values of both variables are equal

Example
x = [1, 2, 3]
y = [1, 2, 3]

print(x == y)
print(x is y)

Membership Operators

Membership operators are used to test if a sequence is presented in an object:

Operator Description Example

in Returns True if a sequence with the specified x in y


value is present in the object

not in Returns True if a sequence with the specified x not in y


value is not present in the object

Bitwise Operators

Bitwise operators are used to compare (binary) numbers:


Operator Name Description

& AND Sets each bit to 1 if both bits are 1

| OR Sets each bit to 1 if one of two bits is 1

^ XOR Sets each bit to 1 if only one of two bits is 1

~ NOT Inverts all the bits

<< Zero fill Shift left by pushing zeros in from the right and let the leftmost bits fall off
left shift

>> Signed Shift right by pushing copies of the leftmost bit in from the left, and let the
right rightmost bits fall off
shift
Python Roadmap for AI – 15 Days
Phase 1 – Core Python (Days 1–7)

Goal: Understand Python basics, data types, and control flow.

Day Topics Focus / Practice

Python Intro, Get Started, Syntax, Install Python & VS Code. Run “Hello World”. Practice
1
Comments comments and basic syntax.

Variables, Data Types (int, float, str,


2 Declare variables, print them, check types.
bool)

3 Operators (+, -, *, /, %, **, //) Practice arithmetic and comparison operators.

4 Strings String methods, slicing, formatting.

5 Lists & Tuples Create, index, slice, modify, loop through them.

6 Sets & Dictionaries Add/remove items, loop through, key-value access.

Conditional statements, logical operators, practice small


7 If…Else & Match
programs.

Phase 2 – Loops & Functions (Days 8–10)

Goal: Automate repetitive tasks and modularize code.


Day Topics Focus / Practice

8 While Loops & For Loops Practice counting loops, nested loops, break/continue.

9 Functions Define functions, parameters, return values.

10 Lambda Functions Simple one-line functions, use in lists/dictionaries.

Phase 3 – Intermediate Python (Days 11–12)

Goal: Handle files, exceptions, modules, and basic OOP.

Day Topics Focus / Practice

11 File Handling Read/write text files, CSV files (small practice).

12 Try…Except & Modules Handle errors, import/use Python libraries.

Phase 4 – Libraries for AI/ML (Days 13–15)

Goal: Start using Python for data handling and visualization.

Day Topics Focus / Practice

13 NumPy Arrays, indexing, slicing, math operations.

14 Pandas Series, DataFrames, read CSV, basic data manipulation.

15 Matplotlib Plot data (line, bar, scatter, histogram), labels, titles.

You might also like