Pythin
Pythin
->Case sensitive
->Concatenation:
print ("Hello " + name)
here, name is a variable.
->Type conversion:
If there's a variable: old_age= 22
Python thinks of the number 22 as string "22". It happens with every variable.
So, if we want to convert it to a int character:
we write it as: int(old_age)
for float: float(old_age)
Matlab mila jula ke variable ke data type change krwane ka tareeka h kyun ki by
default string me leta hn values saare variable unless specified.
->Strings:
name= "Tony Stark"
Upper/lower case= name.upper/lower()
Find Substring= name.find('S') = returns index of S i.e., 5
Index: position starting from 0
Replace= name.replace("Stark", "Ironman")
In keyword= print("T" in name)= returns True
->Arithmetic operators:
print(5-2)
Add: +, Difference: -, Multiply: *, Divide: / (// to remove numbers after
decimal), Modulo: %, Power: **.
Minimise operations: +=, -=
->Comparison operators:
Returns boolean value (True, false)
>, <, <=, >=, ==, !=
->Logical Operators:
Returns Boolean value
or, and, not (true becomes false and vice-versa)
->if-else statement:
age= 19
if age>=18: (colon lagana hota h to show we're gonna write a block of statments)
print("You are an adult") [Indentation is really imp]
print("You can vote")
else:
print("child")
->Range:
->Loop:
1. While:
example:
i=1
while i<=5:
print(i * "*") (jitna i ka value utne stars print kr do)
i= i+1
2. For:
to print sequence. example:
for item in range(5):
print(item+1)
->tuple:
it's a list but immutable i.e., no changes can be made.
marks= (95,98,97,99)
print(marks.count(97)) 97 ka count de dega list me
print(marks.index(97))
->set:
marks= {95,96,97, 97, 97,98}
stores only unique value to agar ham set print krwayenge to 97 ek baar hi aayega
output me.
issme index ni hota to issko unorderd bolte hn, print karane ke liye ye code
likhenge:
for score in marks:
print(score)
->dictionary
key aur value ke pair me value store karta h
marks={"english":95, "chemistry":98}
print(marks["chemistry"])
marks["physics"]= 97; add hojayega physics ka marks dictionary me
->functions
1. Inbuilt function
2. Module function (related funtions and variables stored together for ex: math
module to be imported like: import math/ from math import sqrt (sirf funtion import
krne ke liye module se))
3. User-defined function
syntax:
def funtion_name(parameters):
example:
def print_sum(first, second):
print(first+second)
print_sum(1,2)