Thursday, October 19, 2023

"a = list(a[i] for i in range(0,len(a)))" in python Usage of Generators

 a=[1,2,3,4,5]


k = list(a[x] for x in range(len(a)))

print(k)


a=[1,2,3,4,5]


k = list(a[x] for x in range(len(a)))

print(k)


f = (x for x in a if x%2 == 0) 

print(list(f))

syntax :
variable0 = (variable1 for variable1 in array_name if variable1 % 2 == 0 )
print(variable0)

as above the example inferred single line use of for  loop 

Tuesday, October 17, 2023

Install Java latest JDK OR JRE IN LINUX WINDOWS

 

Download and Install JDK/JRE 7 in Centos & RHEL

$ su -c "yum install java-1.7.0-openjdk"	# JRE Version
$ su -c "yum install java-1.7.0-openjdk-devel"	# JDK version

Download and Install JDK/JRE 8 in Centos & RHEL

$ su -c "yum install java-1.8.0-openjdk"	# JRE Version
$ su -c "yum install java-1.8.0-openjdk-devel"	# JDK version

Download and Install JDK/JRE 9 in Centos & RHEL

$ su -c "yum install java-1.9.0-openjdk"	# JRE Version
$ su -c "yum install java-1.9.0-openjdk-devel"	# JDK version

Download and Install JDK/JRE 7 in Ubuntu

$ sudo apt-get install openjdk-7-jre		# JRE Version
$ sudo apt-get install openjdk-7-jdk		# JDK version

Download and Install JDK/JRE 8 in Ubuntu

sudo apt-get install openjdk-8-jre		# JRE Version
sudo apt-get install openjdk-8-jdk		# JDK version

Download and Install JDK/JRE 9 in Ubuntu

sudo apt-get install openjdk-9-jre		# JRE Version
sudo apt-get install openjdk-9-jdk		# JDK version

Download and Install JDK/JRE 11 in RHEL/CENTOS

# Install New Version
$ yum install java-11-openjdk-devel

Download and Install JDK/JRE 11 in Ubuntu


$ sudo apt-get update
$ sudo apt-get install openjdk-11-jdk -y
$ apt install openjdk-11-jdk-headless -y 

Download and Install JDK/JRE 17 in Ubuntu


$ sudo apt update
$ sudo apt install openjdk-17-jdk
$ sudo apt install openjdk-17-jre
$ java --version

Download and Install JDK/JRE 17 in Centos / RHEL


$ sudo yum install java-17-openjdk
$ sudo yum install java-17-openjdk-devel
$ java -version

How to Remove Java Installed?


# Remove old version
$ java -version
$ yum list installed | grep java
$ yum remove java-1.7.0-openjdk.x86_64
$ yum remove java-1.7.0-openjdk-headless.x86_64
$ yum remove java-1.8.0-openjdk.x86_64
$ yum list installed | grep java
$ yum remove java-1.8.0-openjdk-headless.x86_64
$ java -version

How to download Open Java Packages in Linux?

Location - https://openjdk.java.net/install/

Oracle's OpenJDK JDK binaries for Windows, macOS, and Linux are available on release-specific pages of jdk.java.net as .tar.gz or .zip archives.

As an example, the archives for JDK 13 may be found on jdk.java.net/13 and may be extracted on the command line using following depending on the archive type.

$ tar xvf openjdk-13*_bin.tar.gz
or
$ unzip openjdk-13*_bin.zip

How to set JAVA HOME in Linux System?

$ export JAVA_HOME=/opt/jdk1.8.0_144/
$ export PATH=/opt/jdk1.8.0_144/bin:$PATH;

How to set JAVA HOME env in Windows?


$ set JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
$ set PATH "%PATH%;%JAVA_HOME%\bin";

$ setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
$ setx PATH "%PATH%;%JAVA_HOME%\bin";

Command to set default Java version in Linux?


$ sudo alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.332.b09-1.el7_9.x86_64/jre/bin/java)
   2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64/bin/java)

Enter to keep the current selection[+], or type selection number: 2
[root@sakri1]# java -version

Also, these commands are helpful
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config java_home

break , continue and pass control flow statements

 when to use 'continue','pass','break' 



when you're using 'for loop, while loop,loops' 

inside for loop you can use 'break,continue,pass' if you're using if-else 


Tuesday, October 3, 2023

Dunder Methods _ _variable_ _, _ _method_ _, _ _attribute_ _ In Python

In Python, the double underscore prefix and suffix, such as _ _variable_ _,_ _method_ _,
_ _attribute_ _ are often used to denote special or "magic" methods and attributes. These special methods have specific meanings in the context of object-oriented programming and are used to control various behaviors of objects and classes.

Here are some common use cases for double underscore (dunder) methods and attributes in Python:

Dunder Methods (Magic Methods): _ _variable_ _,_ _method_ _,_ _attribute_ _



_ _init_ _: This method is called when an object is created from a class and is used for initialization.

_ _str_ _: Returns a string representation of an object when str() is called on it.

_ _repr_ _: Returns a string representation of an object for debugging and development purposes. It's called when repr() is used.

_ _len_ _: Returns the length of an object when len() is called on it.

_ _getitem_ _ and _ _setitem_ _: These methods are used to define custom behavior for accessing and setting elements in an object, like in dictionaries or lists.

_ _iter_ _ and _ _next_ _: Used to make an object iterable, allowing it to be used in for loops.

_ _enter_ _ and _ _exit_ _: Used in the context of context managers for resource management with the with statement.

Dunder Attributes:



_ _doc_ _: Contains the docstring (documentation) of a class or function.

_ _name_ _: Holds the name of a module, class, or function.

_ _class_ _: Refers to the class of an object.

_ _module_ _: Contains the name of the module in which a class or function is defined.

These double underscore methods and attributes are part of Python's object-oriented programming features and allow you to define how objects of a class should behave when certain built-in functions or operators are used on them. By implementing these methods and attributes in your classes, you can customize the behavior of your objects to better suit your specific needs.

virtual environment on python on Ubuntu

 # 1. Update system and install Python tools sudo apt update && sudo apt install python3-pip python3-venv -y # 2. Setup Django in a ...