Sunday 7 November 2021

len( ) vs count( ) | Python String

Python String method len( ) is used to get the length of the given string.

name = "Welcome to techies sphere"


print(len(name))

Output: 25 

 

Python String method count( ) is used to get the number of occurrences of a substring in the given string.

string = "Welcome to techies sphere."


subString = "to"


print(string.count(subString)) 

Output: 1

 

Syntax of count( ) method:

string.count( subString, start, end )

 

Parameters:

subString - string whose count is to be found. 

start (Optional) - starting index within the string where count starts. 

end (Optional) - ending index within the string where count ends.

string = "Welcome to techies sphere, to learn and grow."


subString = "to"


print(string.count(subString, 5, 30))

Output: 1


Sunday 3 October 2021

Python strings

A string is a sequence of characters. Which is an array of bytes representing Unicode characters. A string character is simply a string with a length of one. The square bracket [] can be used to access elements of the string.

·    Strings in python are surrounded by either a single quotation mark or a double quotation mark.

·    Like ‘Hello python’ or “Hello python”.

print('Hello Python')

print("Hello Python")



Single-line string

·         To assign a single line string to a variable, use this:

Example

a = "Hello Python"

print(a)



Multiline string

·         To assign the multiline string to a variable, use three single quotes or three double-quotes.

Example

a = '''Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque earum eius, sapiente error quos distincti est recusandae necessitatibus provident'''

print(a)



Example

a = """Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque earum eius, sapiente error quos distincti est recusandae necessitatibus provident"""

print(a)



Note:- The line breaks are inserted at the same position.

Looping a string

·         We can loop through the characters in a string with a “for” loop.

Example

for x in "Hello":

    print(x)



String length

·         The len() function returns the length of string.

Example

a = "Hello Python"

print(len(a))




Thursday 9 September 2021

Program in python

As we learned about syntax in our previous article which is the introduction of python. Python syntax can be executed by writing directly in the command line.

      print("Hello, Python!")

To create a python file in the IDE, we use the .py file extension and run it in the command line.

    c:users\ name >python file.py

Python Indentation

In python, indentation is very important it’s used to indicate a block of code. Where in another programming language the indentation in the code is for readability only.

Example

if 2 > 1:

  print("one is smaller than two!")

Note:- python will give an error if skip the indentation.

Example

Syntax error:

if 2 > 1:

print("one is smaller than two!")

Comment in python

·         It is used to explain the python code.

·         It is used to make the code more readable.

·         It is used to prevent execution when testing code.

·         Create a comment in python with #, and python will ignore them.

 

Example

                #This is a comment

             print("Hello, Python!")

 

Example

      print("Hello, python!") #This is a comment

 

Example

   #print("Hello, Python!")

             print("hello, python3.9!")

Multiline comment

·        Python uses triple quotes for long comments in the python program.

·         To add multi-line comments insert a # for each line.

Example

   #This is a comment

   #written in

   #more then just one line

  print("Hello, Python!")

Example

            """This is a comment

                 written in

                 more than just one line"""

               print("Hello, Python!")


 

 

Wednesday 8 September 2021

Introduction of python

Python is a high-level and most popular programming language. it was created by Guido van Rossum, and released on February 20, 1991.

Uses of python:-

It is used for general-purpose programming like

·         Mathematics

·         System scripting

·         Software development

·         Web development

Why python?

  •          Python works on different-different platforms like Windows, Mac, Linux, etc.
  •          It has a simple syntax.
  •          It has a syntax that allows developers to write the program with fewer lines than some other programming language.
  •          It has an object-orientated way or a functional way.

What can do python?

  •          It can connect to the database system and also read and modify files.
  •         It is used on the server to create the server-side application.
  •          It is used alongside software to create workflows.
  •          It is used to handle big data and performed complex mathematics.

Syntex in python:-

The syntax is the set of rules that defines how a Python program will be written and interpreted by both runtime systems and human readers.

  •       Python uses new lines to complete commands, as opposed to other programming languages which often use semicolons or parentheses.

  •          It relies on indentation, using whitespace, to define the scope of the loop, function, and classes. Other programming languages often use curly-brackets for this purpose.

Example:-

print("Hello Python")

Tuesday 18 May 2021

Linear Recursion in C

If a recursive function is calling itself for only once then, the recursion is linear.
Pseudocode:-
fun(int n)
{


    if (n > 0)
    {


        ...
        fun(n - 1);     // Calling itself only once


        ...
    }


}
Note: The fun() is calling itself for only once.


 
Understanding linear recursion by a simple problem
C program to find a cube of the positive number using Recursion.
#include <stdio.h>
 


int fun(int n)
{


    static int x;
    if (n > 0)


    {
        x++;


        return fun(n - 1) + x * x;
    }


}
int main()


{
    int a;


    printf("number ");
    scanf("%d", &a);


    printf("%d", fun(a));
    return 0;


}
Output:-






 Tracing the above Recursion:-













C program to find the factorial of a positive number using Recursion.


#include <stdio.h>
 


int fun(int n)
{


    if (n == 0)
    {


        return 1;
    }


    else
    {


        return fun(n - 1) * n;
    }


}
 


int main()
{


    int x;
    printf("number ");


    scanf("%d", &x);
    printf("%d\n", fun(x));


    return 0;

}


Output:-



Saturday 1 May 2021

Head recursion in C

If a Recursive function calls itself and  that recursive call is not the last statement of the recursive function then the recursion is known as Head recursion.

 

Structure of Head recursion

int fun(int n)

{

    if (n > 0)

    {

        fun(n - 1);

        ...

        ...

    }

}

 

Note:-  Some statements are executed at return time. 

C program to calculate the factorial of nth number using head recursion.

#include <stdio.h>

 

int fun(int n)

{

    if (n == 0)

    {

        return 1;

    }

    else

    {

        return fun(n - 1) * n;

    }

}

 

int main()

{

    int x;



    printf("number ");

    scanf("%d", &x);

    printf("%d\n", fun(x));

    return 0;

}

Output:-





C program to find the square of any positive number using head recursion.

#include <stdio.h>

 

int fun(int n)

{

    static int x = 0;

    if (n > 0)

    {

        x++;

        return fun(n - 1) + x;

    }

    return 0;

}

int main()

{

    int b;

    printf("number ");

    scanf("%d", &b);

    printf("%d", fun(b));

    return 0;

}

Output:- 






 

Tuesday 27 April 2021

Tail recursion in C

If a recursive function calls itself and that recursive call is the last statement in the function to execute then the recursion is known Tail recursion.

 

Structure of Tail recursion:-

int fun(int n)

{

    if( n >= 0)

    {

       ...

       ...

       fun(n-1);

    }

}

Note: All the statements in the function are executed before the recursive call.

 

Problem:-

C program to count number in descending order using Tail recursion.

 

#include <stdio.h>

 

int fun(int n)

{

    if (n > 0)

    {

        printf("%d\n", n);

        return fun(n - 1);

    }

}

int main()

{

    int a;

    printf("number");

    scanf("%d", &a);

    fun(a);

    return 0;

}

Output:-