Posts

Showing posts with the label Python Example

Array Sort Python Code Example

Example 1: python sort list in reverse #1 Changes list list . sort ( reverse = True ) #2 Returns sorted list sorted ( list , reverse = True ) Example 2: sort array python array = [ 1 , 2 , 3 , 19 , 11 , 90 , 0 ] array . sort ( ) print ( array ) Example 3: sort an array python #List myList = [ 1 , 5 , 3 , 4 ] myList . sort ( ) print ( myList ) #[1,3,4,5] Example 4: sort python >> > x = [ 1 , 11 , 2 , 3 ] >> > y = sorted ( x ) >> > x [ 1 , 11 , 2 , 3 ] >> > y [ 1 , 2 , 3 , 11 ] Example 5: sorting python array sorted ( list , key = . . . , reverse = . . . ) Example 6: python sort list vowels = [ 'e' , 'a' , 'u' , 'o' , 'i' ] vowels . sort ( )

Average Of List Python Code Example

Example 1: average value of list elements in python # Example to find average of list number_list = [ 45 , 34 , 10 , 36 , 12 , 6 , 80 ] avg = sum ( number_list ) / len ( number_list ) print ( "The average is " , round ( avg , 2 ) ) Example 2: python average # Using statistics package to find average import statistics as st my_list = [ 9 , 3 , 1 , 5 , 88 , 22 , 99 ] print ( st . mean ( my_list ) )

Cast Set To List Python Code Example

Example: how to convert a set to a list in python my_set = set ( [ 1 , 2 , 3 , 4 ] ) my_list = list ( my_set ) print my_list >> [ 1 , 2 , 3 , 4 ]

Bot Telegram Python Tutorial Code Example

Example: telegram bot python import telepot from telepot . loop import MessageLoop def handle ( msg ) : content_type , chat_type , chat_id = telepot . glance ( msg ) print ( content_type , chat_type , chat_id ) bot = telepot . Bot ( "INSERT TOKEN HERE" ) bot . message_loop ( handle )

Class And Static Variables In Python Code Example

Example 1: python static variable in function #You can make static variables inside a function in many ways. #____________________________________________________________# """1/You can add attributes to a function, and use it as a static variable.""" def foo ( ) : foo . counter += 1 print ( "Counter is %d" % foo . counter ) foo . counter = 0 #____________________________________________________________# """2/If you want the counter initialization code at the top instead of the bottom, you can create a decorator:""" def static_vars ( ** kwargs ) : def decorate ( func ) : for k in kwargs : setattr ( func , k , kwargs [ k ] ) return func return decorate #Then use the code like this: @static_vars ( counter = 0 ) def foo ( ) : foo . counter += 1 print ( "Counter is %d" % foo . counter ) #___________________________...

Change Column Datatype Of Pandas Dataframe Code Example

Example 1: change dataframe column type >> > df . astype ( { 'col1' : 'int32' } ) . dtypes col1 int32 col2 int64 dtype : object Example 2: set column datatype pandas df = pd . read_csv ( "weather.tsv" , sep = "\t" , dtype = { 'Day' : str , 'Wind' : int64 } ) df . dtypes

Comfort Word To Pdf Code Example

Example 1: word to pdf // In menu of word document select "File" // Under File select "Print" // In Print form , expand the "Printer" selection box . // Select "Microsoft Print to PDF" // Click "Print" and select a destination folder to save your PDF Document . Example 2: word to pdf Install DoPDF . It automatically added to your office programs .

164 Cm In Inches Code Example

Example 1: cm to inch 1 cm = 0.3937 inch Example 2: cm to inches const cm = 1 ; console . log ( `cm : $ { cm } = in : $ { cmToIn ( cm ) } ` ) ; function cmToIn ( cm ) { var in = cm / 2.54 ; return in ; }

Abstraction Python' Code Example

Example: Abstraction example python from abc import ABC , abstractclassmethod class Parent ( ABC ) : @abstractclassmethod def pqr ( self ) : pass class Child ( Parent ) : def pqr ( self ) : print ( "PQR" ) obj = Child ( ) obj . pqr ( )

Ascii Value Of A To Z In Python Code Example

Example 1: how to write the character from its ascii value in python c = 'p' x = ord ( c ) #it will give you the ASCII value stored in x chr ( x ) #it will return back the character Example 2: ascii values in python of c = 'p' print ( "The ASCII value of '" + c + "' is" , ord ( c ) )

59 Prime Number Code Example

Example: first prime numbers #include<bits/stdc++.h> using namespace std ; bool Is_Prime ( long long x ) { if ( x % 2 == 0 ) return false ; for ( int i = 3 ; i * i <= x ; i += 2 ) if ( x % i == 0 ) return false ; return true ; } int main ( ) { // first n prime numbers int n ; cin >> n ; int i = 1 ; while ( n - - ) { while ( !Is_Prime ( + + i ) ) ; cout << i << " " ; } }

C++ Program To Find Gcd Of 3 Numbers Code Example

Example: c++ program to find gcd of 3 numbers #include<stdio.h> int main ( ) { int a , b , c , hcf , st ; printf ( "Enter three numbers : " ) ; scanf ( "%d,%d,%d" , & a , & b , & c ) ; st = a < b? ( a < c?a : c ) : ( b < c?b : c ) ; for ( hcf = st ; hcf >= 1 ; hcf - - ) { if ( a % hcf == 0 & & b % hcf == 0 & & c % hcf == 0 ) break ; } printf ( "%d" , hcf ) ; return 0 ; }

Check If The String Contains Substring In Python Code Example

Example 1: if substring not in string python fullstring = "StackAbuse" substring = "tack" if fullstring . find ( substring ) != - 1 : print "Found!" else : print "Not found!" Example 2: how to check for a substring in python def find_string ( string , sub_string ) : return string . find ( sub_string ) #.find() also accounts for multiple occurence of the substring in the given string Example 3: python check if string contains substring string = "My favourite programming language is Python" substring = "Python" if substring in string : print ( "Python is my favorite language" ) elif substring not in string : print ( "Python is not my favourite language" )

Clear Consoe Python Code Example

Example 1: python clear console import sys , os os . system ( 'cls' ) Example 2: clear console in python As you mentioned , you can do a system call : For Windows >> > import os >> > clear = lambda : os . system ( 'cls' ) >> > clear ( ) For Linux the lambda becomes >> > clear = lambda : os . system ( 'clear' )

9 Cm To.inches Code Example

Example: cm to inches const cm = 1 ; console . log ( `cm : $ { cm } = in : $ { cmToIn ( cm ) } ` ) ; function cmToIn ( cm ) { var in = cm / 2.54 ; return in ; }

Check Lexicographical Order Python Code Example

Example 1: lexicographic order python my_arr = [ "hello" , "apple" , "actor" , "people" , "dog" ] print ( my_arr ) my_arr . sort ( ) print ( my_arr ) Example 2: lexicographic order python my_arr = [ "hello" , "apple" , "actor" , "people" , "dog" ] print ( my_arr ) # Create a new array using the sorted method new_arr = sorted ( my_arr ) print ( new_arr ) # This time, my_arr won't change in place, rather, it'll be sorted # and a new instance will be assigned to new_arr print ( my_arr )

Array Indexof Js Code Example

Example 1: javascript indexof // indexOf getting index of array element , returns - 1 if not found var colors = [ "red" , "green" , "blue" ] ; var pos = colors . indexOf ( "blue" ) ; // 2 // indexOf getting index of sub string , returns - 1 if not found var str = "We got a poop cleanup on isle 4." ; var strPos = str . indexOf ( "poop" ) ; // 9 Example 2: js get index of item in array array . indexOf ( "item" ) ; Example 3: js array get index var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ; return fruits . indexOf ( "Apple" ) ; // Returns 2 Example 4: javasript array indexof var array = [ 2 , 9 , 9 ] ; array . indexOf ( 2 ) ; // 0 array . indexOf ( 7 ) ; // - 1 array . indexOf ( 9 , 2 ) ; // 2 array . indexOf ( 2 , - 1 ) ; // - 1 array . indexOf ( 2 , - 3 ) ; // 0 Example 5: how to get the index ...

Audio Player Javascript Code Example

Example 1: play audio in javascript var audio = new Audio ( 'audio_file.mp3' ) ; audio . play ( ) ; Example 2: play audio javascript var bMusic = new Audio ( 'welcome1.mp3' ) bMusic . play ( ) Example 3: javascript play audio // play audio with from html audio element : document . getElementById ( 'myAudioTagID' ) . play ( ) ; // play audio with out html audio tag var myAudio = new Audio ( 'my_great_song.mp3' ) ; myAudio . play ( ) ; Example 4: javascript play audio // play audio with from html audio element : document . getElementById ( 'myAudioTagID' ) . play ( ) ; // play audio with out html audio tag var myAudio = new Audio ( 'my_great_song.mp3' ) ; myAudio . play ( ) ; Example 5: how play audio js let myAudioElement = new Audio ( 'audio.mp3' ) ; myAudioElement . addEventListener ( "canplaythrough" , event = > { / * the audio is now playable ; play it if permissions al...

Com Fazer A Raiz Quadrada Em C++ Code Example

Example: com fazer a raiz quadrada em c++ #include <stdlib.h> #include <stdio.h> #include <math.h> / * square root of a number * / int main ( ) { float num , raiz ; printf ( "enter a number: \t" ) ; scanf ( "%f" , & num ) ; raiz = sqrt ( num ) ; printf ( "The square root of %f is: %f.\n" , num , raiz ) ; system ( "pause" ) ; return 0 ; }

Aes Encryption Example Python

Example: python AES from Crypto . Cipher import AES import binascii , os import random , string iv = os . urandom ( 16 ) aes_mode = AES . MODE_CBC key = '' . join ( random . choice ( string . ascii_uppercase + string . ascii_lowercase + string . digits ) for _ in range ( 16 ) ) print ( key ) encryptor = AES . new ( key , aes_mode , iv ) def aes_encrypt ( plaintext ) : plaintext = convert_to_16 ( plaintext ) ciphertext = encryptor . encrypt ( plaintext ) return ciphertext def convert_to_16 ( plaintext ) : #Overcome the drawback of plaintxt size which should be multiple of len(iv) add = 16 - ( len ( plaintext ) % 16 ) return ( plaintext + ' ' * add ) Encrypted = aes_encrypt ( 'Jaisal ' ) print ( "Encrypted message :" , Encrypted )