Python notes
Python notes
Print(“hello world)
Input
Input(“What is your name?”)
a = input (“what is your name?”) #this inputs the name into
variable a
Always typecast input since, by default in inputs as string.
String concatenate
Print (“Hello” + a)
Print (“hello “ + input(“what is your name?”))
Str() function convert num to string. Use for concatenating string and
num together.
Len() function gives number of characters in a string
Print(“Hello”[2]) will print the 3rd character (since count starts from 0)
and will print l.
Negative indices are also allowed. [-1] will give the last char, [-2]
will give second last char and so on.
Booleans are not written with quotes
Type() returns the class type of the argument
Str(7) will type cast int into string and this can be used for
concatenate or len() function etc.
Division (/) result is float by default
Division (//) gives int by default. So 7//2 gives answer 3
** gives exponent. 2**3 is equal to 2^3
PEMDAS is followed. M & D are left to right rule. Same for A & S
Int() floors a floating number
Round() rounds the number based on decimal place
Round(x,n) rounds to n decimal places.
fstring
F strings help to join different data types
Suppose
age = 26 #int
height = 1.7 #float
To combine all the different data types we can write as
print(f”My age is {age} and height is {height}”)
#add f at the start of string before quotes and write all variable
inside {} within the string without using any + (string concatenation)
== : check if equal
!= : check if not equal
x+= k, increases x by k
CONDITIONAL STATEMENT (if-else)
if condition:
do this
else:
do this
For multiple conditions:
if condition1:
do this
elif condition2:
do this
elif condition3:
do this
else:
do this
Logical operators
and : both conditions to be true
or : one of the conditions to be true
not : condition to not hold true.