Python :>
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
thislist = ["apple", "banana", "cherry"]
list1 = ["abc", 34, True, 40, "male"]
Lists are used to store multiple items in a single variable
01 for item in thislist:
List items can be of any data type, and can contain
print(item)
different 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
thistuple = ("apple", "apple", "cherry")
Tuples are used to store multiple items in a single variable print(thistuple)
01
A tuple is a collection which is ordered and unchangeable
02 Sets are used to store multiple items in a single variable
A set is a collection which is unordered, unchangeable, and thisset = {"apple", “cherry”, True, 1, 2}
# True and 1 are duplicate
unindexed
print( thisset )
Sets cannot have two items with the same value
MORE DATA TYPES
CONDITIONAL
STATEMENTS
a = 33
An "if statement" is written by using the if keyword b = 200
01
Python relies on indentation (whitespace at the if b > a:
beginning of a line) to define scope in the code instead print("b is greater than a").
of brackets
elif - if previous conditions were not true, then try this condition if b > a:
02
Similarly else can also be used print("b is greater than a")
elif a == b:
print("a and b are equal")
03 The if statements can be nested within each other
if x > 10:
print("Above ten,")
if x > 20 and x<100:
print("Between 20 and 100")
LOOPS
i=1
while i < 6:
print(i)
With the while loop we can execute a set of statements if i == 3:
01
as long as a condition is true break
i += 1
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( fname, lname ):
In Python a function is defined using the def keyword print( fname + " " + lname)
01
Information can be passed into functions as arguments
my_function( “Reyansh”, “College”)
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
Pandas is a Python library used for working with data
sets.
It has functions for analyzing, cleaning, exploring, and
manipulating data.
Thank You
VS CODE
INSTALLATION
Just go to code.visiualstudio.com and download
the desktop version for your OS - (windows/
macOS) --->
complete some basic setup steps to get started If they pop up, just handle them nicely as you do normally
PYTHON INSTALLATION
WINDOWS
Just go to python.org/downloads/ and download
the python interpretor for your Windows
--->
complete some basic setup steps to get started
PYTHON INSTALLATION
MAC OS
Just go to brew.sh and download the homebrew for your mac OS using the command below in your
terminal
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
In your terminal type brew install
python to complete the process
Join CodeWars using the
QR below:
Mentee Registration Form Notion Page