Professional Documents
Culture Documents
Python Fundamentals
Python Fundamentals
Python Fundamentals
VARIABLES
o •Store Data
o •PEP –8
•Dynamically typed –associated with run time values. Values stored in variables have type
and not the name or variable itself
MATH OPERATORS
Comparison Operators –are used to compare values and it either return true or false according
to the condition.
Equal to ==
Not Equal to !=
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=
INTRO TO STRINGS
STRINGS –is an ordered sequence of characters
my_str1 = ‘I Learn Python’
my_str2 = “I Learn Python”
In PyCharmwe need to Use built in function print to display the value of the variable unlike
in python cmd
GET USER INPUT
print('An Example of using input() function')
name = input()
print('Your name is:', name)
BUILT-IN FUNCTION
len() –returns the number of items in an object. When the object is a string, the len() function
returns
the number of characters in a string.
COUNT METHOD
The count() method returns the number of elements with the specific value. Occurrence of a
specific value
Syntax
variableName.count(‘a’)
a = letter or Special character
SUBSCRIPT
Subscript or subscripting is a method of pulling out a particular element from a string. the
number in between the square brackets determines which character you're going to pull
out,andit just goes up from zero to one to two and so on and so forth.
Syntax
variableName[0]
Type Error is an exception that occurs when the data type of an object in an operation is
inappropriate
The Type Checking of the variable type is done at run-time. Also, the type system of the
language doesn't force to explicitly declare the 'data-type' of the variable before its usage.
Type Conversion is the process of converting a data type into another data type
Program Flow Program
If..elif..else
A Block of code is a piece of program that is executed as an unit
If..elif.else
If some_condition_is_true:
o Execute this code
elifsome_other_condition_is_true:
o Execute this code
else:
o Execute the code
If we have a collection of items, iterating over the collection means a way to get items out of the
collection one by one
iterable_object= [1,2,5,9]
foritem initerable_object:
A Range is a type that represents an immutable sequence of numbers and is commonly used
for looping a specific number of times in for loops.
The built in function range() returns ranges of numbers
Range(start, stop, step)
By Default
Start = 0
Step = 1
Start is included
Stop is excluded
Continue
The continue statement in Python returns the control to the beginning of the loop. The
continue statement rejects all the remaining statements in the current iteration of the loop and
moves the control back to the top of the loop.
The continue statement can be used in both while
‘Break’ in Python is a loop control statement. It is used to control the sequence of the loop.
Suppose you want to terminate a loop and skip to the next code after the loop; break will help
you do that. A typical scenario of using the Break in Python is when an external condition
triggers the loop’s termination for loops.
While loop will continue to execute a block of code while some test condition remains True
while some_Boolean_condition_is_true:
do something(while block of code)
else:
another block of code
List and Tuples in Python
LIST
It can hold any object type
Supports Indexing and Slicing. We can refer any item in the sequence by using its index
number
A list is a mutable object and that means it can be modified
A list uses SQUARE BRACKETS and COMMAS to separate elements
list1 = [9, 5, ‘Hello World’, (2, 4)]
list1[2]
‘Hello World’
List Operations
Iteration
print(f'Connectingto {ip}')
Connecting to 192.168.0.1
Connecting to 192.168.0.2
Connecting to 192.168.0.23
list1 += list2 -> concatenates/extends list2 to list1 (doesn’t create a new list)
Assignment (=) operator