Basics
Basic syntax from the python programming language
Showing Output To User
The print function is used to display or print output as follows
u wanna print on s
we can display the content present in object using prit function as follows:-
Pee asa
Pees aa eC)
Taking Input From the User
The input function is used to take input as string or character from the user as follows:
See Gra caer
PSU Gar eee LD)
To take input in form of other datatypes we need to typecaste them as follows:-
To take input as an integer:
ele es tine ta Gre a Me ec anCT)
Peuneeris)
To take input as an float:-
Sele gras eur ria aes ian TT)
print(var1)
range Function
range function returns a sequence of numbers, eg, numbers starting from 0 to n-I for
range(0, n)but int_stop_value is the compulsory parameter in range function
example-
Display all even numbers between | to 100
Comments
Comments are used to make the code more understandable for programmers, and they
are not executed by compiler or interpreter.
Single line comment
Multi-line comment
Escape Sequence
An escape sequence is a sequence of characters; it doesn't represent itself (lout is
translated into another character) when used inside string literal or character. Some of
the escape sequence characters are as follows:
Newline
Newline Character
Backslash
It adds a backslashsINgle Quote
Itadds a single quotation mark
Tab
It gives a tab space
Backspace
Itadds a backspace
Octal value
It represents the value of an octal number
Hex value
It represents the value of a hex number
Pr urt ene)
Carriage Return
Carriage return or \r will just work as you have shifted your cursor to the beginning of the
string or line,String
You can create Strings by enclosing text in both forms of quotes - single quotes or double
quotes.
Reece
example
Indexing
The position of every character placed in the string starts from Oth position ans step by
step it ends at length-1 position
Slicing
Slicing refers to obtaining a sub-string from the given string. The following code will
include index 1, 2, 3, and 4 for the variable named var_name
Slicing of the string can be obtained by the following syntax-
here start and step value are considered 0 and 1 respectively if not mentioned by the
programmmer
isalnum() method
Returns True if all the characters in the string are alphanumeric, else False
Bae U RCE e Lycee)
isalpha() method
Returns True if all the characters in the string are alphabetsISGecimany) Metnoa
Returns True if all the characters in the string are decimals
Bosc mrS coe er caLrTtO)
isdigit() method
Returns True if all the characters in the string are digits
SiS U mC et Ceetc trate)
islower() method
Returns True if all characters in the string are lower case
string_variable.islower()
isspace() method
Returns True if all characters in the string are whitespaces
string_variable. isspace()
isupper() method
Returns True if all characters in the string are upper case
string_variable.isupper()
lower() method
Converts a string into lower case equivalent
Ree mre cL Terr)
upper() methodstrip() method
It removes leading and trailing spaces in the string
strip()
List
A List in Python represents a list of comma-separated values of any data type between
square brackets.
rz Ree OC isms Cicaos
These elements can be of different datatypes
Indexing
The position of every elements placed in the string starts from Oth position ans step by
step it ends at length-1 position
List is ordered,indexed,mutable and most flexible and dynamic collection of elements in
python.
Empty List
This method allows you to create an empty list
aera
index method
Returns the index of the first element with the specified value
-index(element)
append method
Adds an element at the end of the list
Tuo}insert method
Adds an element at the specified position
List. insert (position, element)
pop method
Removes the element at the specified position and returns it
SEC XCrereet)
remove method
The remove() method removes the first occurrence of a given item from the list
List .remove(element)
clear method
Removes all the elements from the list
List.clear()
count method
Returns the number of elements with the specified value
SE Ecae aC cv)
reverse method
Reverses the order of the list
List. reverse()Tuples
Tuples are represented as comma-separated values of any data type within
parentheses.
Tuple Creation
SSC eC usec
These elements can be of different datatypes
Indexing
The position of every elements placed in the string starts from Oth position ans step by
step it ends at length-1 position
Tuples are ordered,indexing,immutable and most secured collection of elements
Lets talk about some of the tuple methods:
count method
It returns the number of times a specified value occurs in a tuple
ert)
index method
It searches the tuple for a specified value and returns the position.
-index(value)
Sets
Aset is a collection of multiple values which is both unordered and unindexed. It is written
in curly brackets.
Set Creation: Way 1set creation. Way 2
[element1
Set is unordered,immutable,non-indexed type of collection Duplicate elements are not
allowed in sets.
Set Methods
Lets talk about some of the methods of sets:
add() method
Adds an element to a set
Rea)
clear() method
Remove all elements from a set
-clear()
discard() method
Removes the specified item from the set
Re Oren)
intersection() method
Returns intersection of two or more sets
payee tas Cs etc)
issubset() method
Checks if a set is a subset of another setPOPPY Metmnoa
Removes an element from the set
remove() method
Removes the specified element from the set
union() method
Returns the union of two or more sets
-union(set1, set2...)
Dictionaries
The dictionary is an unordered set of comma-separated key-value pairs, within {}, with
the requirement that within a dictionary, no two keys can be the same.
Dictionary
Dictionary is ordered and mutable collection of elements.Dictionary allows duplicate
values but not duplicate keys.
Empty Dictionary
By putting two curly braces, you can create a blank dictionary
Adding Element to a dictionary
By this method, one can add new elements to the dictionaryVpaaung Element i @ Gicuonary
Ifa specified key already exists, then its value will get updated
crests Bec
Deleting an element from a dictionary
del keyword is used to delete a specified key-value pair from the dictionary as follows:
erasC Nea RIES
Dictionary Functions & Methods
Below are some of the methods of dictionaries
len() method
It returns the length of the dictionary, i.e, the count of elements (key: value pairs) in the
dictionary
(dictionary)
clear() method
Removes all the elements from the dictionary
dictionary.clear()
get() method
Returns the value of the specified key
dictionary. get(keyname)
items() method
Returns a list containing a tuple for each key-value pairKeys), Mewnog
Returns a list containing the dictionary’s keys
dictionary. keys()
values() method
Returns a list of all the values in the dictionary
dictionary.values()
update() method
Updates the dictionary with the specified key-value pairs
Ciastir inn cites uri)
Indentation
In Python, indentation means the code is written with some spaces or tabs into many
different blocks of code to indent it so that the interpreter can easily execute the Python
code.
Indentation is applied on conditional statements and loop control statements. indent
specifies the block of code that is to be executed depending on the conditions.
Conditional Statements
The if, elif and else statements are the conditional statements in Python, and these
implement selection constructs (decision constructs).
if Statement
(conditional expression):
ere
if-else Statementif-elif Statement
aout eeu vas
ere
CSEE a Coot eet yy
Bec
Orr
statem
if(a>b and apc):
print(a,"is
BrCae Tn TS))
print(b," is greatest)
ro
eu Careers
Loops in Python
A loop or iteration statement repeatedly executes a statement, known as the loop body,
until the controlling expression is false (0).RSC Iet SSMS Tete
Be ema
example-
[ees user
print(i)
While Loop
A while loop is a conditional loop that will repeat the instructions within itself as long as a
conditional remains true.
while