LEARN GOLANG QUICKLY AND PYTHON CODING PRACTICE EXERCISES Coding For Beginners (TAM, JJ) (Z-Library)
商业Author:TAM, JJ
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
LEARN GOLANG QUICKLY AND PYTHON CODING PRACTICE EXERCISES CODING FOR BEGINNERS WITH HANDS ON PROJECTS BY J J TAM
Page
3
Introduction Go Language Go: Hello World Application Go language: Build executable Go: Primitive Data Types Go language: Print value and type of a variable Go language: Initialize multiple variables in one line Go Language: Constants Go language: iota identifier Go Language: Type Conversion Go language: Type Inference Go language: strings Go language: concatenate strings Go language: multi line strings Go Language: Arrays Go Language: Arrays are passed by value Go language: Slices Go language: Slices are passed by reference Go language: Iterate over a slice using range keyword Go language: Get slice from a slice Go language: Append an element to a slice Go Language: Map data structure Go language: Define map using literal notation Go language: len : Get number of items in a map Go Language: Map: Check whether an item exists in map or not
Page
4
Go language: Delete item from the map Go language: Print all the keys of map Go language: Print key, value from map Go language: if statement Go language: if-else statement Go language: if statement: combine initialisation and condition evaluation in same line Go Language: switch statement Go language: switch without an expression Go Language: for loop Go Language: Use for loop as while loop Go language: Print elements of array Go language: Iterate over a map Go language: Read data from console or user Go Language: Create a function Go Language: Pass by Reference Go language: Variadic Functions Go language: Return a value from function Go language: Anonymous functions Go Language: Functions continued Go Language: return Error from a function Go Language: Structs Go Language: Construct an object to a structure using & Go language : struct literal notation Go language: constructor functions
Page
5
Go Language: Adding Methods to a struct PYTHON String – Exercises Python: Reverse a string Remove a newline in Python Find the common values Python Data Type: List – Exercises Python: Sum all the items in a list Get the largest number from a list Remove duplicates from a list Python Data Types: Dictionary – Exercises Add a key to a dictionary Iterate over dictionaries using for loops Merge two Python dictionaries Multiply all the items in a dictionary Remove a key from a dictionary Get a dictionary from an object's fields Combine two dictionary adding values for common keys Find the highest 3 values PYTHON BASICS – EXERCISES Display current date and time Print the calendar Computes the value of n+nn+nnn Calculate number of days volume of a sphere in Python
Page
6
Compute the area of Triangle Compute the GCD CALCULATE THE LCM Convert feet and inches to centimeters Convert time – seconds Convert seconds to day Program to solve Future value of amount Check whether a file exists Convert the distance Sum all the items Multiplies all the items Get the largest number Get the smallest number Remove duplicates Clone or copy a list Difference between the two lists Generate all permutations Find the second smallest Get unique values Get the frequency of the elements Generate all sublists Find common items Create a list
Page
7
LEARN GOLANG QUICKLY CODING FOR BEGINNERS WITH HANDS ON PROJECTS BY J J TAM
Page
8
Introduction Go Language Go is an open source language to build reliable software in efficient way. a . Go is a compiled language. b . Go has clean and clear syntax c . Go has in built language support for concurrency d . Go is statically typed language e . Functions are first class citizens in Go f . Go initialize default values for uninitialized variables. For example, for the string default value is empty string. g . Go has good features that makes the development fast h . Go has only few keywords to remember i . Most of the computers now a days has multiple cores, but not all languages have efficient ways to utilize these multi cores. But Go has very good support to utilize multi core system in efficient way. . Since Go has very good built-in support for concurrency features, you no need to depend on any threading libraries to develop concurrent applications. k . Go has in-built garbage collector, so you no need to take the overhead of managing application memory. j. In Go, complex types are composed of smaller types. Go encourages composition.
Page
9
Install and setup Go Download latest version of Go from below location. https://golang.org/ Extract the downloaded zip file, you can see below content structure. $ ls AUTHORS CONTRIBUTORS PATENTS VERSION bin favicon.ico misc robots.txt test CONTRIBUTING.md LICENSE README.md api doc lib pkg src Add bin directory path to your system path. Open terminal or command prompt and execute the command ‘go’, you can see below output in console. $ go Go is a tool for managing Go source code. Usage: go <command> [arguments] The commands are: bug start a bug report build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol
Page
10
env print Go environment information fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages or modules mod module maintenance run compile and run Go program test test packages tool run specified go tool version print Go version vet report likely mistakes in packages Use "go help <command>" for more information about a command. Additional help topics: buildmode build modes c calling between Go and C cache build and test caching environment environment variables filetype file types go.mod the go.mod file gopath GOPATH environment variable gopath-get legacy GOPATH go get goproxy module proxy protocol importpath import path syntax modules modules, module versions, and more module-get module-aware go get packages package lists and patterns testflag testing flags testfunc testing functions Use "go help <topic>" for more information about that topic.
Page
11
Note If you do not want to install Go in your system, you can play around Go at ‘https://play.golang.org/’.
Page
12
Go: Hello World Application Open any text editor and create HelloWorld.go file with below content. HelloWorld.go package main func main() { println ( "Hello, World" ) } Execute the command ‘go run HelloWorld.go’. $ go run HelloWorld.go Hello, World package main It is used by the Go compiler to determine application entry point. func main() Program execution starts from here. ‘func’ keyword is used to define a function. ‘main’ function do not take any arguments. println("Hello, World") ‘println’ is a built in function in Go, that is used to print given message to console. Note a . Unlike C, C++ and Java, you no need to end a statement by a semi colon. b . String in Go, are placed in between double quotes
Page
13
c . Strings in Go are Unicode.
Page
14
Go language: Build executable Use the command ‘go build goFilePath’, to build the executable from go program. App.go package main import "fmt" func main() { fmt.Println( "Hello World" ) } $ go build App.go $ $ ls App App.go As you see 'App' file is created after building. If you run build command in windows, it generates App.exe file. Run the file 'App' to see the output. $ ./App Hello World
Page
15
Go: Primitive Data Types Below table summarizes the data types provided by Go Language. Integer Data Types Data Type Description Minimum Value Maximum Value uint8 Unsigned 8- bit integers 0 255 uint16 Unsigned 16-bit integers 0 65535 uint32 Unsigned 32-bit integers 0 4294967295 uint64 Unsigned 64-bit integers 0 18446744073709551615 int8 Signed 8-bit integers -128 127 int16 Signed 16- bit integers -32768 32767 int32 Signed 32- bit integers -2147483648 2147483647 int64 Signed 64- bit integers -9223372036854775808 9223372036854775807 Below table summarizes floating point numbers. Data Type Description float32 IEEE-754 32-bit floating-point numbers float64 IEEE-754 64-bit floating-point numbers
Page
16
Below table summarizes the complex numbers. Data Type Description complex64 Complex numbers with float32 real and imaginary parts complex128 Complex numbers with float64 real and imaginary parts Apart from the above types, Go language support below types that are implementation specific. a . Byte b . rune (same as int32) c . uint d . int e . uintptr Syntax to create variable var variableName dataType var variableName dataType = value variableName := value HelloWorld.go package main import "fmt" func main() { var a int = 10 var b uint8 = 11 var c uint16 = 12 var d uint32 = 13 var e uint64 = 14 var f int8 = 15 var g int16 = 16
Page
17
var h int32 = 17 var i int64 = 18 var k float32 = 2 var l float64 = 2 complex1 := complex ( 10 , 13 ) fmt.Println( "a : " , a); fmt.Println( "b : " , b); fmt.Println( "c : " , c); fmt.Println( "d : " , d); fmt.Println( "e : " , e); fmt.Println( "f : " , f); fmt.Println( "g : " , g); fmt.Println( "h : " , h); fmt.Println( "i : " , i); fmt.Println( "k : " , k); fmt.Println( "l : " , l); fmt.Println( "complex1 : " , complex1); } Output a : 10 b : 11 c : 12 d : 13 e : 14 f : 15 g : 16 h : 17 i : 18 k : 2 l : 2 complex1 : (10+13i) How to access real and imaginary numbers from complex numbers?
Page
18
You can use ‘real’ and ‘img’ methods to access the real and complex parts of a number. myComplex := complex(10, 13) real(myComplex) imag(myComplex) HelloWorld.go package main import "fmt" func main() { complex1 := complex ( 10 , 13 ) var realPart = real (complex1) var imgPart = imag (complex1) fmt.Println( "complex1 : " , complex1); fmt.Println( "realPart : " , realPart); fmt.Println( "imgPart : " , imgPart); } Output complex1 : (10+13i) realPart : 10 imgPart : 13
Page
19
Go language: Print value and type of a variable %v is used to print the value of a variable %T is used to print the type of a variable. App.go package main import "fmt" func main() { var x int = 10 fmt.Printf( "i : %v, type : %T\n" , x, x) } Output i : 10, type : int Go language: Initialize multiple variables in one line If variables are of same data type, you can initialize them in one line like below. var i, j, k int = 1, 2, 3
Page
20
App.go package main import ( "fmt" ) func main() { var i, j, k int = 1 , 2 , 3 fmt.Println( "i : " , i, ", j : " , j, ", k : " , k) } Output i : 1 , j : 2 , k : 3
Comments 0
Loading comments...
Reply to Comment
Edit Comment