Basic syntax from the Python programming language
TheΒ print
Β function is used to display or print output as follows:
print("Content that you wanna print on screen")
var1 = "Sid"
print("Hi my name is: ", var1)
var1 = input("Enter your name: ")
print("My name is: ", var1)
To take input in the form of other data types, we need to typecast them as follows:
var1 = int(input("Enter the integer value"))
print(var1)
var1 = float(input("Enter the float value"))
print(var1)
range(int_start_value, int_stop_value, int_step_value)
Example:
for i in range(0, 101, 2):
print(i)
Comments are used to make the code more understandable for programmers, and they are not executed by the compiler or interpreter.
# This is a single line comment
'''This is a
multi-line
comment'''
An escape sequence is a sequence of characters that doesn’t represent itself but is translated into another character when used inside a string literal or character. Some of the escape sequence characters are as follows:
Newline Character:
print("\n")
print("\\")
print("\'")
print("\t")
print("\b")
It represents the value of an octal number:
print("\ooo")
print("\xhh")
print("\r")
Python string is a sequence of characters, and each character can be individually accessed using its index.
variable_name = "String Data"
str = "Sid"
print("string is ", str)
The position of every character placed in the string starts from the 0th position and step by step it ends at length-1 position.
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.
string_var[int_start_value:int_stop_value:int_step_value]
var_name[1:5]
string_variable.isalnum()
string_variable.isalpha()
string_variable.isdecimal()
string_variable.isdigit()
string_variable.islower()
string_variable.isspace()
string_variable.isupper()
string_variable.lower()
string_variable.upper()
string_variable.strip()
var_name = [element1, element2, ...]
The position of every element placed in the list starts from the 0th position and step by step it ends at length-1 position. List is ordered, indexed, mutable, and the most flexible and dynamic collection of elements in Python.
my_list = []
list.index(element)
list.append(element)
list.extend(iterable)
list.insert(position, element)
list.pop(position)
list.remove(element)
list.clear()
list.count(value)
list.reverse()
list.sort(reverse=True|False)
variable_name = (element1, element2, ...)
tuple.count(value)
tuple.index(value)
A set is a collection of multiple values which is both unordered and unindexed. It is written in curly brackets.
var_name = {element1, element2, ...}
var_name = set([element1, element2, ...])
Set is an unordered, immutable, non-indexed type of collection. Duplicate elements are not allowed in sets.
Let’s talk about some of the methods of sets:
set.add(element)
set.clear()
Removes the specified item from the set:
set.discard(value)
set.intersection(set1, set2 ... etc)
set.issubset(set)
set.pop()
Removes the specified element from the set:
set.remove(item)
set.union(set1, set2...)
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-name> = {<key>: value, <key>: value ...}
Dictionary is an ordered and mutable collection of elements. Dictionary allows duplicate values but not duplicate keys.
By putting two curly braces, you can create a blank dictionary:
mydict = {}
<dictionary>[<key>] = <value>
If a specified key already exists, then its value will get updated:
<dictionary>[<key>] = <value>
del <dictionary>[<key>]
Below are some of the methods of dictionaries:
len(dictionary)
dictionary.clear()
dictionary.get(keyname)
dictionary.items()
dictionary.keys()
dictionary.values()
dictionary.update(iterable)
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.
if (conditional expression):
statements}
if (conditional expression):
statements
else:
statements
if (conditional expression):
statements
elif (conditional expression):
statements
else:
statements
if (conditional expression):
statements
elif (conditional expression):
statements
else:
statements
if (conditional expression):
if (conditional expression):
statements
else:
statements
else:
statements
a = 15
b = 20
c = 12
if (a > b and a > c):
print(a, "is greatest")
elif (b > c and b > a):
print(b, " is greatest")
else:
print(c, "is greatest")
for <variable> in <sequence>:
statements_to_repeat
while <logical-expression>:
loop-body
i = 1
while (i <= 100):
print(i)
i = i + 1
for in :
statement1
if :
break
statement2
statement_after_loop
Example:
for i in range(1, 101, 1):
print(i, end=" ")
if (i == 50):
break
else:
print("Mississippi")
print("Thank you")
for in :
statement1
if :
continue
statement2
statement3
statement4
Example:
for i in [2, 3, 4, 6, 8, 0]:
if (i % 2 != 0):
continue
print(i)
def my_function():
# statementst
my_function()
def add(): # function definition
a = 10
b = 20
print(a + b)
add() # function call
return [value/expression]
def my_function(arg1, arg2, arg3, ...argn):
# statements
my_function(arg1, arg2, arg3, ...argn)
Example:
def add(a, b):
return a + b
x = add(7, 8)
print(x)
var_name = open("file name", "mode")
var_name.close()
read() # return one big string
return [value/expression]
def my_function(arg1, arg2, arg3, ...argn):
# statements
my_function(arg1, arg2, arg3, ...argn)
Example:
def add(a, b):
return a + b
x = add(7, 8)
print(x)