q Learn python in easy step. Python Basic Syntax
Learn python in easy step. Python Basic Syntax

Learn python in easy step. Python Basic Syntax

Python Variables

Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

For Example:

X=10

Y=’’Jhon”

Print(x)

Print(y)

Output:

10

Jhon

Code run in vs code with screenshot:

 

Python For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Example:

fruits = ["apple", "banana", "cherry"]

for x in fruits:

  print(x)

Code run in vs code:


 

Python Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

Example:

def my_function():

  print("Hello from a function")

Code run in vs:

Arguments:

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

def b(fname):

  Print(fname )

b("america")

Code run in vs code:

 

Number of Arguments:

By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.

Example:

Def a(fname, lname)

Print(fname + lname)

a(“Aelina”+ “” +”Rai”)

Output: Aelina Rai

Code run in vs :

 

Python Lambda

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

Syntax:

lambda arguments: expression

The expression is executed and the result is returned:

For example:

Add 10 to argument a, and return the result:

x = lambda a : a + 10

print(x(5))

Lambda functions can take any number of arguments:

Example

Multiply argument a with argument b and return the result:

x = lambda a, b : a * b

print(x(5, 6))

Example

Summarize argument a, b, and c and return the result:

x = lambda a, b, c : a + b + c

print(x(5, 6, 2))

Code run in vs code :

 

Python Arrays

An array is a special variable, which can hold more than one value at a time. Arrays are used to store multiple values in one single variable:

Example

Create an array containing car names:

cars = ["Ford", "Volvo", "BMW"]

Example

Get the value of the first array item:

x = cars[0]

The Length of an Array:

Use the len() method to return the length of an array (the number of elements in an array).

Example

Return the number of elements in the cars array:

x = len(cars)

Looping Array Elements:

You can use the for in loop to loop through all the elements of an array.

Example

Print each item in the cars array:

for x in cars:

  print(x)

Adding Array Elements:

You can use the append() method to add an element to an array.

Example

Add one more element to the cars array:

cars.append("Honda")

Removing Array Elements:

You can use the pop() method to remove an element from the array.

Example

Delete the second element of the cars array:

cars.pop(1)

Code run in vs code :