Python 101
Python 101
Presented By
WnCC
VARIABLES
Variables are containers for storing data values x=5
01
A variable is created the moment you first assign a value y = "John"
to it
Variables do not need to be declared with any particular x=4 # x is of type int
02
type, and can even change type after they have been set. x = "Sally" # x is now of type str
The data type of a variable can be specified with casting x = str(3) # x will be '3'
03
y = float(3) # y will be 3.0
z = int(input())
PRINT
print() function prints the specified message to the screen by
01
converting it into a string >>>name = "Alice"
>>>age = 30
>>> print("Hello", "how are you?", sep="---") >>>print(f"Name: {name}, Age: {age}")
Hello---how are you? Name: Alice, Age: 30
BASIC PYTHON VARIABLES
N
COLLECTION DATA TYPES
A list within another list is referred to as a nested list matrix = [ [1, 2, 3],
02
[4, 5, 6],
[7, 8, 9] ]
Dictionaries are used to store data values in key:value thisdict = { "brand": "Ford",
03
pairs "electric": False,
As dictionaries cannot have two items with the same key, "year": 1964,
"colors": ["red", "blue"] }
values are basically mapped to keys
COLLECTION DATA TYPES
The range() function returns a sequence of numbers, for x in range(2, 30, 5):
02
starting from 0 by default, and increments by 1 (by print(x)
default), and ends at a specified number, until specified
otherwise
A for loop is also used for iterating over a sequence (that adj = ["red", "big", "tasty"]
03
is either a list, a tuple, a dictionary, a set, or a string)
for x in adj:
print(x)
FUNCTIONS
def my_function(x):
02 To let a function return a value, use the return
return 5 * x
statement:
def tri_recursion(k):
03 Python also accepts function recursion, which means a
if(k > 0):
defined function can call itself result = k + tri_recursion(k - 1).
Be carefull of writing a function which never terminates print(result)
else:
return 0
CONTROL STRUCTURE
CLASSES
class Dog:
sound = "bark
It bundles data and functions together, making it easier
01 to use them dog1 = Dog()
When we create a new class, we define a new type of print(dog1.sound)
object, which can be created multiple times
class Dog:
species = "Canine"
02 In Python, class has __init__() function. It automatically def __init__(self, name, age):
initializes object attributes when an object is created self.name = name
self.age = age
class Dog:
03 Functions can also be created inside a class
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} is barking!").
Library:
Numpy
NumPy is a Python library.
NumPy is used for working with
arrays.
NumPy is short for "Numerical
Python".
list vs numpy
Library:
Numpy arrays : Seed, Slicing
array,Array creation, array
math,matrices, random numbers,
Let’s deep dive ,
scan it
Pandas
complete some basic setup steps to get started If they pop up, just handle them nicely as you do normally
PYTHON INSTALLATION
WINDOWS