PYTHON BASICS

***Numbers***
# Python supports two types of numbers - integers and floating point numbers
my_integer=int(7.98765)
print(my_integer)

my_float=float(7)
print(my_float)

# Expression syntax is straightforward: the operators +, -, * and /
# parentheses (()) can be used for grouping

print(2+2)
print(4-2*5)
print((4-2*5)/2)
print((4-2)*5/2)

# The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float
# Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result)

# you can use the // operator; to calculate the remainder you can use %
print(14/3) # classic division returns a float
print(14//3)    # floor division discards the fractional part
print(14%3) # the % operator returns the remainder of the division

# With Python, it is possible to use the ** operator to calculate powers
print(5**2)
print(2**7)

# There is full support for floating point; operators with mixed type operands convert the integer operand to floating point
print(4*3.75-1)




***Strings***
# Strings are defined either with a single quote or a double quotes
my_string='mohanraja'
print(my_string)
my_string='mohanraja\'s'
print(my_string)

my_string="bharathiyar"
print(my_string)
my_string="bharathiyar \"swami\""
print(my_string)

# The difference between the two is that using double quotes makes it easy to include apostrophes
my_string="I don't use the name here!!"
my_string="Don't worry about apostrophes"
print(my_string)

# Uses of \n and escaping with \
my_string="first line is 1\nsecond line is 2"
print(my_string)

my_string=r"first line is 1\nsecond line is 2"
print(my_string)

# String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''
multiple_lines="""My name\
is Mohanraja and I have create a\
blog for learning and developement"""
print(multiple_lines)

multiple_lines='''My name
is Mohanraja and I have create a
blog for learning and developement'''
print(multiple_lines)

# Strings can be concatenated (glued together) with the "+" operator, and repeated with "*"
con_rep=3 * "mohan" + 3 * "raja"
print(con_rep)

# Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated
without_con_rep='py' 'thon'
print(without_con_rep)

# This only works with two literals though, not with variables or expressions
# If you want to concatenate variables or a variable and a literal, use "+"
py="Python"
print(py+"turorial")

# Strings can be indexed (subscripted), with the first character having index 0.
# There is no separate character type; a character is simply a string of size one
word_indexed="Mohanraja"
print(word_indexed[0])

# Note that since -0 is the same as 0, negative indices start from -1
# Indices may also be negative numbers, to start counting from the right:
print(word_indexed[-4])
print(word_indexed[0:5])

# The built-in function len() returns the length of a string
print(len(word_indexed))




***Lists***
"""Python knows a number of compound data types, used to group together other values.
The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets.
Lists might contain items of different types, but usually the items all have the same type"""

list=[1,4,9,16,25]
print(list)

# Like strings (and all other built-in sequence type), lists can be indexed and sliced
print(list[2:])
print(list[-2:])

# Lists also support operations like concatenation
print(list+[36,45,54,21])

# Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content
multipleby2=[2,4,6,8,9]
print(multipleby2)
multipleby2[4]=10     # replacing wrong value
print(multipleby2)

# You can also add new items at the end of the list, by using the append() method
multipley2=multipleby2.append(12)
print(multipleby2)
multipley2=multipleby2.append(2*7)
#print(multipleby2.append(2*7))
print(multipleby2)

# Assignment to slices is also possible, and this can even change the size of the list or clear it entirely
letters=['a','b','c','d','e']
letters[2:4]=[2,3]
print(letters)

# The built-in function len() also applies to lists
print(len(letters))

# It is possible to nest lists (create lists containing other lists)
x=['a','b','c']
y=['1','2','3']
z=[x,y]
print(z)
print(z[0][2])