Python3

# single line comment


'''

this is multiline

comment

'''


"""

variable rules:-

variable names are case sensitive (name and NAME are different variables)

must start with a letter or an underscore

can have numbers but can't start with one

"""


x=1

y=2.5

name='hemanth'

is_cool = True


x,y,name,is_cool = (1,2.5,'hemanth',True)


print(type(x))


print(x)


# casting

a= float(x)






Strings:-


# strings in python are surrounded by single or double quotes.


name = 'hemanth'

age = 28


print('hello, my name is '+name+' I am '+str(age))


# Arguments by position

print('my name is {name} and I am {age}'.format(name=name, age=age))


# f-strings(3.6+)

print(f'hello i am {name} and i am {age}')




Strings:-


s = 'hello world'


# capitalize string

print(s.capitalize())


# make all uppercase

print(s.upper())


# make all lower

print(s.lower())


# swap case

print(s.swapcase())


# get length

print(len(s))


#replace

print(s.replace('world', 'everyone'))


# count

sub = 'h'

print(s.count(sub))


# starts with

print(s.startswith('hello'))


# ends with

print(s.endswith('d'))


# split into a list

print(s.split())


# find position 

print(s.find('r'))


# is all alphanumeric

print(s.isalnum())


# is all alphabetic

print(s.isalpha())


# is all numeric

print(s.isnumeric())





# A list is a collection which is ordered and changeable. Allows duplicate members [].

# a tuple is a collection which is ordered and unchangeable. Allows duplicate memberes().

# a set is a collection which is unordered and unindexed. No duplicate members{}.

# A dictionary is a collection which is unordered, changable and indexed. No duplicate members.







List:-

# A list is a collection which is ordered and changeable. Allows duplicate members.


# create list

numbers=[1, 2, 3, 4, 5]


# use a constructor

numbers2 = list((1, 2, 3, 4, 5))


print(numbers, numbers2)


fruits = ['apples', 'oranges', 'grapes', 'pears']


# get a value

print(fruits[1])


# get length

print(len(fruits))


# append to list (end of list)

fruits.append('mangos')


# remove from list

fruits.remove('grapes')


# insert into position

fruits.insert(2, 'strawberries')


# change value

fruits[0] = 'blueberries'


# remove with pop

fruits.pop(2)


# reverse list

fruits.reverse()


# sort list

fruits.sort()


# reverse sort

fruits.sort(reverse=True)


print(fruits)





Tuple:-


# a tuple is a collection which is ordered and unchangeable. Allows duplicate memberes.


# create tuple

fruits = ('apples', 'oranges', 'grapes')


fruits2 = tuple(('apples', 'oranges', 'grapes'))


print(fruits, fruits2)


# single value needs trailing comma

fruits2 = ('apples',)


# get value

print(fruits[1])


# delete tuple

#del fruits2


#can't change value

#fruits[0] = 'pears'


# get length

print(len(fruits))


print(fruits2)







set:-


# a set is a collection which is unordered and unindexed. No duplicate members.


# create set

fruits_set = {'apples', 'oranges', 'mango'}


# check if in set

print('apples' in fruits_set)


# add to set

fruits_set.add('grape')


# remove from set

fruits_set.remove('grape')


# add duplicate(no duplicate)

fruits_set.add('apples')


# clear set

fruits_set.clear()


# delet

del fruits_set






Dictionary:-


# A dictionary is a collection which is unordered, changable and indexed. No duplicate members.


# create dict

person = {

    'first_name': 'hemanth',

    'last_name': 'vajrapu',

    'age': 28

}


# use constructor

person2 = dict(first_name='hemanth', last_name='vajrapu')


print(person, type(person))


# get value

print(person['first_name'])


print(person.get('last_name'))


# get dict keys

print(person.keys())


# get dict items

print(person.items())


# add key/value

person['phone'] = '555-555-5555'


# copy dict

person2 = person.copy()

person2['city']='Boston'


print(person2)


# remove item

del(person['age'])

person.pop('phone')


# clear

person.clear()


# get length

print(len(person2))


# list of dict

people = {

    {'name': 'hemanth', 'age':28},

    {'name':'sai', 'age': 28}

}


print(people[1]['name'])







Functions:-

# a function is a block of code which only runs when it is called. In python, we do not use curly brackets,

we use indentation with tabs or spaces



def sayHello(name):

    print(f'hello {name}')


sayHello('hemanth vajrapu')




def sayHello(name='hemanth'):

    print(f'hello {name}')


sayHello()



def sayHello(name):

    print(f'hello {name}')


sayHello('hemanth vajrapu')




# return values

def getSum(num1, num2):

    total = num1+num2

    return total

print(getSum(3, 4))




def getSum(num1, num2):

    total = num1+num2

    return total

num = getSum(3, 4)

print(num)





# A lambda function is a small anonymous function

# A lambda function can take any number of arguments, but can only have one expression. Very similar to js arrow functions



getSum=lambda num1,num2:num1+num2

print(getSum(10,3))



# logical operators (and, or, not) - used to combine conditional statements

x =10

y=10


# and

if x>2 and x>=10:

    print(f'{x} is greater than 2 and less than or equal to 10')


# or

if x>2 or x<=10:

    print(f'{x} is greater than 2 or less than or equal to 10')


# not

if not(x==y):

    print(f'{x} is not equal to y')




# membership operators (not, notin) - membership operators are used to test if a sequence is presented in an object


x=20


numbers = [1,2,3,4,5]


# in

if x in numbers:

    print(x in numbers)


# notin

if x not in numbers:

    print(x not in numbers)


# identity operators (is, is not) - compare the objects, not if they are equal, but if they

are actually the same object, with the same memory location:


x=20

y=20


# is

if x is y:

    print(x is y)


# is not 

if x is not y:

    print(x is not y)






# a for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set or a string)


for i in range(1, 10):

    print(i)


for i in range(10):

    print(i)




people = ['hemanth', 'sai', 'ganesh','kumar']



for i in range(len(people)):

    print(people[i])


for person in people:

    print(f'Current person: {person}')



#break

for person in people:

    if person == 'ganesh':

        break

    print(f'Current person: {person}')


#continue

for person in people:

    if person == 'ganesh':

        continue

    print(f'Current person: {person}')



# while


count = 0

while count <=10:

    print(f'count: {count}')

    count+= 1



i = 1

while i<=10:

    print(i)

    i+=1


# break

i = 1

while i<=10:

    if i==6:

        break

    print(i)

    i+=1


#continue

i = 0

while i<=10:

    i+=1

    if i==6:

        continue

    print(i)

Comments

Popular posts from this blog

Social Engineering

In one lifetime, you will love many times, but one love will burn your soul forever.

The cost of growth is pain.