0% found this document useful (0 votes)
64 views

Int-It Represents All Number Float - It Represents Numbers With Decimal Values STR - It Represents All Letters

The document provides information on various Python concepts including data types, operators, strings, and functions. 1. It discusses the different numeric data types in Python - integers for whole numbers, floats for decimal numbers, and how to perform basic math operations and convert between types. 2. It covers strings - how to define, concatenate and format them. Special characters and escape sequences in strings are also explained. 3. Various functions related to strings and numbers are demonstrated including len(), type(), round(), abs(), bin(), int(), etc. Operator precedence and augmented assignment are also covered.

Uploaded by

Vikram Bij
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Int-It Represents All Number Float - It Represents Numbers With Decimal Values STR - It Represents All Letters

The document provides information on various Python concepts including data types, operators, strings, and functions. 1. It discusses the different numeric data types in Python - integers for whole numbers, floats for decimal numbers, and how to perform basic math operations and convert between types. 2. It covers strings - how to define, concatenate and format them. Special characters and escape sequences in strings are also explained. 3. Various functions related to strings and numbers are demonstrated including len(), type(), round(), abs(), bin(), int(), etc. Operator precedence and augmented assignment are also covered.

Uploaded by

Vikram Bij
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Python

Int- It represents all number

Float- it represents numbers with decimal values

Str- it represents all letters


Basic math functions

print (type(5 + 5))


print (type(9 - 4))
print (type(9 * 5))
print (type(6 / 3))

print (type(6.5 +5.5))


print (type(11.9 + 1.1))

print (2**4) 2 raise to the power 4


print (5**6) number of times multiplies by self or 5 raise to the power 6
print (400//10)
print (5//4) shows the nearest integer 1,2,3,4,5 etc.
print (6%4) shows remainder of the division

<class 'int'> 5

<class 'int'> 45

<class 'float'> 3.0

<class 'float'>11.0

<class 'float'> 13.0

16

15625

40

1 - it is the nearest integer //

2 it is remainder %

Note – Python 3 math numbers


#MATH FUNCTIONS
print (round (3.1)) 3 rounds off
print (round (3.9)) 4 rounds off
print (abs (-20)) 20 absolute value (positive value of any number)

OPERATOR PRECEDENCE

#operator precdence
20 + 3 * 4 - here first 3 will be multiplied by 4 and then 20 will be added

#OPERATOR PRECDENCE
print (20 - 3 * 4) 8
print ((20 - 3) * 4) 68
print ((20 - 3) + 2**2) 21
# ()
# **
# * /
# + -
1. Complex numbers and binary representation of numbers, base number
is 2(base 2 numeral system 0s and 1s)
#complex - its a third type instead of int and float

#Binary representation of number

print (bin(5)) #binary number for 5 is 101, and b in result means binary, it shows
its a binary number. 0b101- binary representation

print (int('0b101', 2)) #2 is the base number 5

print (bin(12)) 0b1100

print (int('0b1100', 2)) 12

print (bin(44)) 0b101100

print (int('0b101100', 2)) 44

0b101

0b1100

12

0b101100

44

VARIABLES
#VARIABLES
iq1 = 190
#here the iq of person is 190 and 190 is asigned to iq, so the variable here is iq.
print (iq1)

user_iq = 190 #variables must start with either lowercase or an _(underscore) and
they must be in snake case, which means instead of space we use underscore.
print(user_iq)

_user_iq = 190
print (_user_iq)

us4r_iq34 = 190
print (us4r_iq34)

#note - undersccore in pyhton means private variable

#keywords should not be repeated, blue colored words are print words.example

print = 190
print (print)
#there will be a error if you use this

_____________________________________________________________________________________

iq = 190
user_age = iq/4
print (user_age) result = 47.5

#CONSTANTS
PI = 3.14
PI = 0
print (PI)

#we use PI in capital to shows that its not meant to be changed. and later we
reassigned the value of PI so it shows 0 in result instead of 3.14. we can reassign
any value.

#dunder variables starts from 2 underscores. and they should not be touched. we can
assign them value but dont practice with them
__hihi = 'hihi'
print (__hihi) result = hihi
#trick for assigning multiple values together to a variables

a,b,c = 1,2,3
print (a,b,c) result = 1,2,3

a,b,c = 1,2,'wow'
print (a) 1
print (b) 2
print (c) wow

iq = 100
user_age = iq/5
print (user_age, iq) 20.0 100

#here expression is iq/5, expression is code which produces value and statement is
the whole term, so we have two expression in above code. iq=100 and user......

_____________________________________________

#AUGEMENTED ASSIGNMENT OPERATOR

some_value = 5
some_value = some_value + 5 #u can also do some_value = 5 + 2
print (some_value) 10

#augemented assignemt operator helps in doing this above thing more neatly

some_value = 5
some_value += 2 7
print (some_value)

some_value1 = 23
some_value1 *= 2 46
print (some_value1)

here the operator before the equal mark does the same thing as shown above, operator
must be on the left side of the equal sign
STRING – all words in python are called strings

#strings it must be in "" or ''

print (type('hiii this is surya 1')) <class 'str'>


username = 'supercoder'
password = 'supersecret'
print (username) supercoder
print (password) supersecret

#we can write strings through both marks, but there is a third way to write them too,
that is for long strings that is multiple lines

long_string = '''
WOW
O O
---
'''
print (long_string) WOW
O O
---

first_name = 'Surya'
last_name = 'Singh'
full_name = first_name + ' ' + last_name Surya Singh
print (full_name)
#up here u wont get space so we will add another string in full name with space or u
can put a space at the end of surya in first name or at the beggining of singh in
last name

#string concatenation - it means adding strings together


print ('hello' + ' Surya')

#string concatenation only workd with string u cant add a number like 5 at the place
of surya
#TYPE CONVERSION
#how to make a string file read as a integer and vice versa.

print (type(str(100))) <class 'str'>


#now 100 is an integer but right now its being read as SyntaxWarning

print (type(int(str(99)))) <class 'int'>


#now we first made it read as string and later as integer so overall integer.

#or

a = str(54)
b = int(a)
c = type(b)
print (c) <class 'int'>

# ESCAPE SEQUENCE
weather = "it\'s \"kind of\" sunny"
print (weather) it's "kind of" sunny

#here adding \ before ' and " means the coming sign would be used assumed to be a
string.

weather1 = "\t It\'s sunny \n hope you have a good day!"


print (weather1) It's sunny
hope you have a good day!
#here \t means adding a tab Space, \n means new line
weather2 = "It\'s \\kinda sunny"

a = "its,\ suuny"

print (weather2) It's \kinda sunny

print (a) its,\ suuny

#FORMATTED STRINGS

(suppose u work for amazon and a new user logs in)

name = 'Johnny Cash'


age = 55

print ('Hiii ' + name + '\nYou\'re ' + str(age) + ' years old')

result - Hiii Johnny Cash


You're 55 years old

#now to display this name johnny on profile page we can do it by formatted string
#now the above info lookjs complicated so we will write an f at the beginning of the
string. it will tell python its a formatted string.

_____________________________________________________________________________________

#formatted strings

name = 'Surya Singh'


age = 19
print (f'Hii {name}. \nYou\'re {age} years old.')
#this f is a new feature of pyhton 3, so for python 2 we used following method

name1 = 'Vikram Bijarnia'


age1 = 20
print ('Hii {}. \nYou\'re {} years old.'.format('Vikram Bijarnia', '20'))

name2 = 'Ayushi Choudhary'


age2 = 19
weight2 = 20
print ('Hii {}. \nYou\'re {} years old. \nYour weight is {} kilogram (That\'s too
less you skinny girl)'.format (name2, age2, weight2))
name3 = 'Ayushi Choudhary'
age3 = 19
weight3 = 30
print ('Hii You\'re {1} years old. \nYour name is {0}. \nYour weight is {2} kilogram
(Still low you skinny girl)'.format (name3, age3, weight3))

#now a basic method without using formatted string

name4 = 'Ayuhsi Choudhary'


age4 = 19
weight4 = 32
print ('Hii ' + name4 + '. \nYou\'re ' + str(age4) + ' years old' + '\nYour weight is
' + str(weight4) + ' kilograms'+ ' (You\'re stil skinny)')

Results for above code

Result –
Hii Surya Singh.
You're 19 years old.

Hii Vikram Bijarnia.


You're 20 years old.

Hii Ayushi Choudhary.


You're 19 years old.
Your weight is 20 kilogram (That's too less you skinny girl)

Hii You're 19 years old.


Your name is Ayushi Choudhary.
Your weight is 30 kilogram (Still low you skinny girl)

Hii Ayuhsi Choudhary.


You're 19 years old
Your weight is 32 kilograms (You're stil skinny)

INDEXES\

selfish = '01234567'
#01234567
# [start:stop:stepover]
print (selfish[0]) #says start from zero
print (selfish[3]) #says start from 3 counting as 0,1,2,3
print (selfish[0:4]) #says start from 0 and and stop at 4. 4 is not readed.
print (selfish[0:8])
print (selfish[0:8:1]) #means start from 0 till 8 and jumover 1 number, for example
first zero, jumping 1 no, its 1 so read 1, then jump and read 2.
print (selfish[0:8:2]) #means start from 0 till 8 by jumoiing on 2 numbers, for
example first its 0, then jump 2 nnumber, it reaches 2 so read 2 then jump again 2
number it reaches 4, so read 4.....
print (selfish[1:]) #it says start from 1 and till deafault so means all number
print (selfish[:5]) #means start at default 0 all the way to 5. 5 is not read
print (selfish[-1]) #in pyhton negative index means start at the end.
print (selfish[::-1]) #it means start at default stop at default and stepover from
back. it gives us reverse.
print (selfish[::3])
print (selfish[::-2])

RESULTS – 0
3
0123
01234567
01234567
0246
1234567
01234
7
76543210
036
7531

#IMMUTABILITY
#a value in string cannott be reassigned, you have to assign it completely

name = 'Surya'
name = 'Ayushi'
print (name)
#here we completely assigned a new value, but what we if we assign a new value to a
part of string, lets see

marks = '214791'
marks[2] = '6'
print (name + marks)
#here we get an error saying string does not support item assignment, if you want to
change the value then u need to completely reassign it.
income = 3456
income[3] = 9
print (income)
#so integer also does not support item assignment

Results - Ayushi
Traceback (most recent call last):
File "main.py", line 10, in <module>
marks[2] = '6'
TypeError: 'str' object does not support item assignment

#BUILT IN FUNCTION
#1 – len

greet = 'hellloooo'

print (greet[0:len(greet)]) # = [o:9]

#BUILT IN METHOD - SOME EXAMPLES


#1 - .upper() - makes it uppercase
#2 - .capitalize() - capitalize first letter
#3 - .lower() - lowercases everything
#4 - .find() - to check if the given letter exists
#5 - .replace() - it replaces the first letter with the one mentioned next

quote = 'to be or no to be'


print (quote.upper())
print (quote.capitalize())
print (quote.lower())
print (quote.find('be'))
print (quote.replace('be', 'me'))
print (quote)
RESULTS - TO BE OR NO TO BE
To be or no to be
to be or no to be
3
to me or no to me
to be or no to be

#PASSWORD CHECKER
#exercise on password checker

username = input('What is your username?')


password = input('What is your password?')

password_length = len(password)
hidden_password = '*' * password_length
print(f'{username}, your password, {hidden_password} is {password_length} letters
long.')

Result - What is your username?Ayushi


What is your password?Iamskinny
Ayushi, your password, ********* is 9 letters long.
LIST

LIST

#it contains all different type of data


#li = [1,2,3,4]
#li2 = ['a', 'b', 'c', 'd']
#li3 = [1, 'e', 2.5, True]

amazon_cart = ['notebook', 'sunglasses', 'pen']


print (amazon_cart[0])
print (amazon_cart[1])

LIST SLICING

amazon_cart = ['notebook',
'sunglasses',
'pen',
'toys',
'grapes',
]
amazon_cart[0] = 'laptop'
new_cart = amazon_cart[0:3]
new_cart[0] = 'gum'

print (new_cart)
print (amazon_cart)

#NOTE - List is immutable, here above new cart has created a copy of amazon cart so
amazon cart is not changed but if you remove the slicing braackets [] then it will
overwrite. to completely copy amazon cart without any changes use this [:] as shown
below

walmart_cart = [
'cheese',
'brownies',
'ice cream',
'chips'
]
walmart_cart[0] = 'ghee'
walmart_side_cart = walmart_cart[:]
walmart_side_cart[0] = 'coconut oil'

print (walmart_side_cart)
print(walmart_cart)

Results-

['gum', 'sunglasses', 'pen']


['laptop', 'sunglasses', 'pen', 'toys', 'grapes']
['coconut oil', 'brownies', 'ice cream', 'chips']
['ghee', 'brownies', 'ice cream', 'chips']

#MATRIX - its a array with another array inside it.

matrix = [
[1,5,1],
[0,1,0],
[1,0,1]
]

print (matrix[0][1])
#here we wanted to access the first item of whole array and the that array's 2nd
item.

# actions we can take on lists


#length is for humans so its not counted from Zero
#using methods

basket = [1,2,3,4,5]
print (len(basket))

#adding method - append - used to add sth


list_1 = [1,2,3,4,5,6]
new_list = list_1.append(100)
new_list = list_1
print(new_list)

#here append 100 just adds 100 but it does not produce result it just changes the
list.

#insert - to insert sth, we need to first write index where we want to insert then
the object

list_2 = [1,3,5,7]
list_2.insert(4, 9) #here 4 is 7 so 9 will be added in place of 7 and will come on
place of 4 and 7 will move to spot 5
list_2.insert(0, 2) #here 2 will be on place zero, so 1 will move to place 1.
list_2.insert(1, 100)
list_2.insert(3, 99)
print(list_2)

result - 5
[1, 2, 3, 4, 5, 6, 100]
[2, 100, 1, 99, 3, 5, 7, 9]

#extending the list, it modifies the list

basket = [1,3,5,7,9]
new_list = basket.extend([100,101])
basket.pop()
basket.pop(0) #o will remove whatever is on place 0 or index
print(basket)
print(new_list)

#removing - pop pops off whatever is at the end of list


basket_1 = [1,3,5,6,7,8,11,23,435]
basket_1.pop()
basket_1.pop()
basket_1.remove(3)
print (basket_1)

#pop needs index of the data which u want to remove like 0,1,2,3 etc. but remove
needs the value, you put the value and it wil be removed.

#extend does not return anything so does remove, but pop returns thr value it popped
off.

#clear removes all the values in the list

basket_2 = [13,14,23,53,654,765,8]
basket_2.clear()
print (basket_2)

Result =
[3, 5, 7, 9, 100]
[3, 5, 7, 9, 100]
None
[1, 5, 6, 7, 8, 11]
[]

basket = ['a','b','x','c','d','e','e','a']
print(basket.index('d', 0, 5))
print(basket.index('b', 0 ,4))

print('a' in basket)
print('f' in basket)
print('i' in 'hii my name is adrien')

print(basket.count('d'))
print(basket.count('e'))

print(sorted(basket))
print(basket)
#sorted means this in sort form
new_basket = basket[:] #or
new_basket_1 = basket.copy()
new_basket_1.sort()
print(new_basket_1)
new_basket.sort()
print(new_basket)

basket.sort()
print(basket)

basket.reverse()
print(basket)

#to check if a item is list we use in


#.count is to find how many times this letter has been used in list
#sort- it sorts the list, it modifies the list, if a is after e in list, then
it will sort it to before as u can see above
#sorted - another way to sort the list
#the difference between sort and sorted is that sorted prints prints new
array, so if you print basket again, then it will come in its orginal form.
it does not modifies the list or array unlike sort.sorted
#reverse starts the arraay from back.

RESULT –
4
1
True
False
True
1
2
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['a', 'b', 'x', 'c', 'd', 'e', 'e', 'a']
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['a', 'a', 'b', 'c', 'd', 'e', 'e', 'x']
['x', 'e', 'e', 'd', 'c', 'b', 'a', 'a']

basket = ['a', 'b', 'c', 'd', 'e', 'f', 'd']


basket.sort()
basket.sort()
print(list(range(101)))

sentence = ''
new_sentence = sentence.join(['hi ', 'my ', 'name ', 'is ', 'JOJO.'])
print(new_sentence)
#range helps us to create a numbered list real quick.
#.join works on string, an empty string

Result –
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
hi my name is JOJO.

#list unpacking
a,b,c,*other,d = [1, 2, 3, 4, 5 , 6, 7, 8, 9]
print(a)
print(b)
print(c)
print(other)
print(d)

Result –
1
2
3
[4, 5, 6, 7, 8]
9

#Dictionary - python calls it dict- its a data type but also data structure,
curly brackets denotes dictionary, it has a key and a value

dictionary = {
'a' : 1,
'b' : 'Hellooo',
'd' : [1,2,3],
'c' : True
}

my_list = [{
'a' : [1,2,3],
'b' : 'Hiiii',
'c' : 4,
'd' : True
},
{
'a' : [5,6,8],
'b' : 'Heyyyo',
'c' : True,
'd' : 5

}]
print(dictionary['b'])

print(dictionary)

print(my_list[1]['a'][1])

Result –

Hellooo
{'a': 1, 'b': 'Hellooo', 'd': [1, 2, 3], 'c': True}
6

#dictionary keys are immuatble


#keys need to be unique, if u use same key it will overwrite last one
user = {
'123' : [1,2,3,4],
'Greeting' : 'Hellooo',
'age' : 34,
23 : 43

}
print (user['123'])
print(user[23])
print(user['age'])
print(user.get('weight', 23))

user_2 = dict(name = 'Ayushi')


print(user_2)

Result –

[1, 2, 3, 4]
43
34
23
{'name': 'Ayushi'}

user = {
'name' : 'Ayushi',
'basket_items' : ['Grapes', 'Apples', 'Biscuits'],
'age': 19,
'weight' : 45
}
print(user.keys())
print(user.items())
print('age' in user.keys()) #checking keys
print( 19 in user.values()) #checking value

print(user.pop('name'))
print(user) #pop changes the actual list
print(user.popitem()) #it randomly pops of any key and value, in this case its weight

print(user.update({'age': 20}))
user.update({'skinny_level': 10})

#user.update either updates the existing key's value or creats a new key and valu if
the same do not exist.

user_2 = user.copy()
user.clear()
print(user_2)
print(user)

RESULT –

dict_keys(['name', 'basket_items', 'age', 'weight'])


dict_items([('name', 'Ayushi'), ('basket_items', ['Grapes', 'Apples', 'Biscuits']),
('age', 19), ('weight', 45)])
True
True
Ayushi
{'basket_items': ['Grapes', 'Apples', 'Biscuits'], 'age': 19, 'weight': 45}
('weight', 45)
None
{'basket_items': ['Grapes', 'Apples', 'Biscuits'], 'age': 20, 'skinny_level': 10}
{}

You might also like