DSA
DSA
DATA STRUCTUTRE AND ALGORITHM
1.Printing
Printing and then moving cursor to next line
JS
console.log("hello sharpnerians");
Python
print("hello sharpnerians")
C++
cout<<"hello sharpnerians"<<endl;
Java
System.out.println("hello sharpnerians");
Printing and cursor remaining same line
JS
not possible
Python
print("hello sharpnerians", end="")
C++
cout<<"hello sharpnerians";
Java
System.out.print("hello sharpnerians");
2.Variables
its a placeholder to store values
x=3 //x is the variable
Datatype
its defined type of the data to be stored
primitive data type=it has value
Static defined datatype:
In some language, datatype should be mention
e.g. int x=5 //java
Dynamic defined datatype:
e.g. x=5 //python
python:
def calculate(a,b, c): 5 """write the code inside this block to calculate a+b-c""" 6 print(a+b-c) 7 8 9 10 11 """Dont change anything below. If changed click on reset.""" 12 13def main(): 14 a = int(input()) 15 b = int(input()) 16 c = int(input()) 17 calculate(a,b,c) 18main()
x=8
y=3
print(x)
operator in python
print(x/y)
o/p=2.8
print(x//y)
o/p=2
--------------------------------------------------------------------------------------------
swap two numbers:
def swap(a,b): 5 """write the code inside this block to swap two numbers""" 6 x=b 7 b=a 8 a=x 9 10 11 """Dont change anything below. If changed click on reset.""" 12 print("a value is =",a) 13 print("b value is =",b) 14 15def main(): 16 a = int(input()) 17 b = int(input()) 18 swap(a,b) 19main()
IF ELIF ELSE
x=3
if x==3:
print("true")
else:
print("false")
IF ELSE LADDER
def print_cost(distance): 5 """ 6 write the code below to print the cost 7 if the distance is given 8 """ 9 if distance<=100: 10 print("5") 11 elif distance>100 and distance<=500: 12 print("8") 13 elif distance>500 and distance<=1000: 14 print("10") 15 elif distance>1000: 16 print("12") 17
TERNERY OPERATOR
(if else statement in a single line)
java:
x = a<10 ? 5 :10
if(a<10)
x=5;
else
x=10;
python:
syntax: opt1 if condition else opt2
e.g. 5 if a<10 else 10
a = "xxx" if x<6 else "yyy"
e.g. x=6
print(10 if x>0 else 1)e.g. x=5 a = "xxx" if x<6 else "yyy" print(a)
SWITCH STATEMENT
LOOP
Execute particular statement continuously till the condition will reach
While loop:
initialization
condition
incrementation
e.g.
i=1
while i<=5:
print("Hello world")
i=i+1
e.g. Print all even numbers from 1 to n
def print_even(n): 5 6 i=2 7 while i<=n: 8 print(i) 9 i=i+2 10 11def main(): 12 n=int(input()) 13 print_even(n) 14 15main()
--------------------------------------------------------------------------------------------
FOR LOOP
Syntax:
for i in range(start, end inc/dec)
e.g.
for i in range(2, 11, 2):
print(n);
o/p
2 4 6 8 10
for i in range(10,1, -2):
print(n)
o/p 10 8 6 4 2
for i in range(25, -1,-5):
print(n)
o/p 25 20 15 10 5 0
e.g. for loop used with list
x=["sumi",34,1223]
for i in x:
print(x)
o/p sumi 34 1223
x="sumi"
for i in x:
print(i)
o/p s u m i
for i in x["sumi",23,455]
print(i)
o/p sumi 23 455
for i in range(10):
print(i)
o/p 012345678910
pattern
1st loop ------ pointing no. of lines
2nd loop-------pointing details of each line
for(10,
m=-1
y=mx+c
y=-1x+c
14=-1*2+c
c=16
formula =16-i
inner loop(10,16-i,1)
def print_message(message): 13 14 print(message) 15 16def main(): 17 while True: 18 try: 19 message = input() 20 21 except EOFError: 22 break 23main()
-------------------------------------------------------------------------------------------
Array
Dynamic Array
BIT MANIPULATION
*1 byte = 8 bitsuse of Right shift:
Find maximum of array without using inbuilt fun max():
Time Complexity
return-1unit
***************
Method | Description |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isascii() | Returns True if all characters in the string are ascii characters |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
Sislower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Joins the elements of an iterable to the end of the string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
File "demo_string_format_error.py", line 2, in <module>
txt = "My name is John, I am " + age
TypeError: must be str, not int
To extract a specific word from a string in Python, you can use the split()
method to split the string into a list of words, and then access the desired word using its index.
Here's an example:
- text = "The quick brown fox jumps over the lazy dog."
- word_index = 3
- extracted_word = text.split()[word_index]
- print(extracted_word) # Output: "fox"
In this example, text.split()
returns a list of words ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
, and word_index = 3
selects the 4th word in the list, which is "fox".
Comments
Post a Comment