Strings in Python

What is String in python?
  • In Python, String is a sequence of characters enclosed by either single quotes or double-quotes.
    • Example: 'Python', "Geek4Tutorial"
  • Strings are immutable.
  • Each value in a string is called character. 
Creating a String:
  • Strings can be created by enclosing the sequence of characters inside a single quote or double quote and assigning it to a variable.
Strings in Python
  • Sometimes triple quotes can also be used to create a string, even though triple quotes are used to represent Multiline strings and Docstrings.
Strings in Python
  • We can access the characters(element) in the string just like the index using index position.

Example python programs: Functions basics

Simple python programs to practice:

1) Write a Python function Factorial(n) which returns the factorial of the given number(n).

2) Write a Python function, Square(num) which returns the square of the given number(num).

3) Write a Python function, Sum(n) which returns the sum of first n numbers.

4) Write a Python function, Palindrome(num) that accepts an integer num as input and returns true/false based on condition.

    Ex: num = 12321 ---> output = 12321 ----> then given number is palindrome.

5) Write a Python function, check_strong_number(num) that accepts a positive as input, and returns true/false based on condition.
  • A number is said to be a Strong number if the sum of the factorial of each digit is equal to the given number.
   Ex: 145 is strong number as (1! =1) + (4! = 24) + (5! = 120) = 145.

Lists in Python

What is a List?
  • The list is one of the most common collection datatypes in Python which is ordered and mutable (changeable).
  • It is used to store a group of an element together in a sequence.
  • A list can store both homogenous and heterogeneous elements i.e., a single list may contain data types like integers, floats, strings as well as objects.
    • Individual value in a list is called an element.
    • These elements will be stored in a contiguous memory location.
      • For Ex:
        • Number1 = 10
        • Number2 = 20
        • Number3 = 30
        • Number4 = 40 
    • These individual variables can be represented in a list as
      • Number_list = [ 10, 20, 30, 40 ]

    Functions in Python basics

    Functions In python:
    • Set of instructions to perform a specific task.
    • Below is the syntax of functions in python:
                       def function_name([arg1,....,arg n]):
                              statement 1
                              ........
                              statement n
                              [return value]
                       variable = function_name([value1,......,value n])

    Control Structures

    • In general, Control structures decide the flow of the program execution.
    • The most commonly used control structures in python are
        • Sequential: control flows through all the statements, in the order of how the code is written.
        • Selection: Depends on the condition, control flows to the different statements.
        • Iteration: Certain blocks of code(statements) will be executed repeatedly.

      You might also like

      Deploy your Django web app to Azure Web App using App Service - F1 free plan

      In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...