C initialize an array

  • Array is collection of similar data items.
  • An array can hold multiple values of same type of data
  • In c programming language an array can be declared as datatype arrayName [ arraySize ]; 
  • In c , while declaring an array we need to specify type of array , name of array and size of the array.
  • One can directly initialize an array while declaring itself with some values
  • int arrayVariable[2]=[10,20];
  • int count[]=[1,2,3,4,5]
  • directly initialize an array of particular : count[3]=10
  • Let us discuss about how to initialize an array with some default values.
  • Using for loop or while loop we can iterate an array and by accessing each index we can assign values 
  • If we are not initializing an array and try to print all the values then it will print some garbage values.
  • c initialize array of ints

#1Write a C program to declare an array and print default values
  1. #include <stdio.h>
  2. // write a c program to explain how to initialize an array
  3. int main () {

  4.    int number[ 15 ]; 
  5.    int i,k;
  6.    
  7.    
  8.    for (k = 0; k < 15; k++ ) {
  9.       printf("Array[%d] = %d\n", k, number[k] );
  10.    }
  11.  
  12.   getch();
  13. }


Output:
  1. Array[0] = -1551778920
  2. Array[1] = -2
  3. Array[2] = 1971427546
  4. Array[3] = 1971495162
  5. Array[4] = 5189464
  6. Array[5] = 5181048
  7. Array[6] = 2686776
  8. Array[7] = 1971494685
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 2686832
  12. Array[11] = 200
  13. Array[12] = 192
  14. Array[13] = 7161720
  15. Array[14] = 48


#2 write a C Program to initialize an array values to 0 (zeros).

initialize an array in c


Output:
  1. Array[0] = 0
  2. Array[1] = 0
  3. Array[2] = 0
  4. Array[3] = 0
  5. Array[4] = 0
  6. Array[5] = 0
  7. Array[6] = 0
  8. Array[7] = 0
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 0
  12. Array[11] = 0
  13. Array[12] = 0
  14. Array[13] = 0
  15. Array[14] = 0

spilt function in python

 Split function: 

  • The split() function is used to split the string by separating them with the split().
  • The split()  method contains the different separators  like comma(,) colon(:) semicolon(;)  and space also.
  • By default when we write split() function it will separate the values by "space ".
  • After splitting the string it returns a list of strings.
Syntax: str.split(separator,max split)
  • Example:
  • y=[int(x) for x in input("enter the 2 numbers:").slipt(",")]
  • Here, the 2 numbers are 10 2 by using split(",") these numbers are separated by"," as 10,5.
  • There are  3 parameters in the split() method
  1. separator: It separates the string by using separators if we do not use then by default it takes space.
  2. max split:  It is used to split the string by the max number of times. By default, it takes 1 i.e no limit for splitting. 
  3. Returns: After splitting the string it returns the list of string values.

# write a python code using the split().

  1. a="apple is a fruit"
  2. b=" dog is a animal"
  3. c="python is easy to learn"
  4. print(a.split())
  5. print(b.split())
  6. print(c.split())
output: ['apple' , 'is' , 'a' , 'fruit' ]
            ['dog'  ,'is'  ,'a', 'animal']
            ['pthon'  , 'is'   , 'easy'  ,'to' , 'learn']



python Ternary operator

Ternary operator: 

  • The ternary operator is defined as the operator that evaluates the conditions to find the final result.
  • It evaluates more than one expression.
  • A  single line code is written according to the condition.
syntax:  x= first value if condition else second value.
In this syntax, if the first value satisfies the condition then it returns the x value, if the first value doesn't satisfy the condition then it goes to the else part and checks the second condition.

Example: x=20 if 5<10 else 30 we get the result as 20.
Because here condition 5<10 is grue so it returns the x value as 10.

  • There is another syntax for the ternary operator when there are more than 2 conditions i.e;
  • syntax: x= first value if condition 1 else second value if condition 2 else third value.
Example: x=1 if 20>30 else 2 if 50>60 else  10. 
                The result is  10.
  • In the ternary operator, we can perform a nested loop also.
  • We can reduce the length of the code by using this ternary operator, in this operator we write a code in a single line.
  • So time is saved and less complexity in the code.

#write a python program for integers using the ternary operator.

  1. a=8
  2. b=9
  3. max= b if b>a else a.
  4. print(max)
output: 9



python not operator

 Python not operator:

  • python not operator is the complement of the given value i.e; if the value is true then it returns false and if the value is false then it returns true.
  • When we  apply the not operator we get result  in  boolean type .
  • example:
                 x=10 
                 not x, then the result is false because x=10 is true so not x is false.
  • syntax:(not x).

 # write a python program using not operator.

  1. x="python"
  2. y="java''
  3. print(not x==y)
output: true

 

python and operator

 python And operator:

  • The python And operator is one of the logical operators.
  • In And operator, if both the operands are true then it returns true, otherwise false.
  • And operator is applicable for boolean type and non-boolean type.
  • For example: 1 And  " a"  then the result is "a".  
  •  It is represented by the  "&" symbol.
  •  It is written as  x &y
syntax:(a&b).

# write a python program using And operator.

  1. x=23
  2. y=44
  3. print(x>10 and y>10)
  4. print(x<40 and y<40)
  5. print(x&y)
  output:   true
                false
                 4

Types of operators in python

 Operators: 

  • Operators are nothing but the special symbols used in between the operands.
  • In python, we use the operators to perform some arithmetic, logic, and some more operations.
  • These operations are performed on the variables, values, arguments nothing but the operands.
  • We can perform operations on one or more operands.
  • In python, we have six operators.
  • Arithmetic( to add),comparison(to compare), logical( perform logical operations), bitwise(cal bitwise operations), assignment(assign values), special(check address and content ).

1.Arithmetic operator:

  • It is also called a math operator and it applicable for all int and float types values.
write a python program using the math operators/arithmetic operators.



2. comparison operator:

  • It compares and shows the relation of variables.
write a python code using comparison operators.




3. Logical operator: 

  •  This operator performs the logical operations on variables.
  • There are 3 logical operators.
write a python program using logical operators.



4. Bitwise operator:

  • It is used to perform bit-wise calculations.
  • We have 6 operators and applied to int and bool.
write a python code using bitwise operators.



5. Assignment operator:

  • It assigns the values to variables.
  • we have 12 operators in it.
write a python program using the assignment operators.



6.Special operator:

  • In, not in and is, is not are special operators.
  • Membership operators and identity operators are 2 types in it.
  • Membership operator is used to giving member is present in the list or not.
  • eg: list=[1,2,3]
  • pint(1 is a list )
  • output: true.
write a python program using the special operator.



Select Menu