0% found this document useful (0 votes)
5 views3 pages

Julia Cheat Sheet BW Clean

This document is a cheat sheet for the Julia programming language, covering basic syntax, data types, control flow, functions, collections, broadcasting, modules, and string operations. It includes examples of comments, variable declarations, loops, function definitions, and file I/O. Additionally, it provides performance tips for optimizing code execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Julia Cheat Sheet BW Clean

This document is a cheat sheet for the Julia programming language, covering basic syntax, data types, control flow, functions, collections, broadcasting, modules, and string operations. It includes examples of comments, variable declarations, loops, function definitions, and file I/O. Additionally, it provides performance tips for optimizing code execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Julia Cheat Sheet

Basic Syntax and Data Types

# Comments and Printing


# This is a comment
println("Hello, world!")

# Variables and Types


x = 42 # Int
y = 3.14 # Float64
s = "Hello" # String
b = true # Bool
typeof(s) # Returns String

Control Flow

# If-Else
if x > 0
println("Positive")
elseif x == 0
println("Zero")
else
println("Negative")
end

# Loops
for i in 1:5
println(i)
end

while x > 0
x -= 1
end

Functions and Scope

# Function Definition
function add(a, b)
return a + b
end

# One-liner
square(x) = x^2

# Anonymous Function
f = x -> x * 2

# Scope
let x = 5
println(x)
end

Collections

# Arrays
a = [1, 2, 3]
a[1] # Access first element
push!(a, 4) # Add element
Julia Cheat Sheet

# Tuples
t = (1, "hello", 3.0)

# Dictionaries
d = Dict("a" => 1, "b" => 2)
d["a"] # Get value
Julia Cheat Sheet

Broadcasting and Comprehensions

# Broadcasting (element-wise)
[1, 2, 3] .+ 1 # [2, 3, 4]
sin.([0, pi/2, pi]) # [0.0, 1.0, 0.0]

# List Comprehension
[x^2 for x in 1:5] # Squares
[(x, y) for x in 1:2, y in 1:2] # Pairs

Modules, Packages and Macros

# Using Modules
using Statistics, LinearAlgebra

# Install Packages
using Pkg
Pkg.add("Plots")

# Define Module
module MyUtils
export greet
greet() = println("Hi!")
end

# Useful Macros
@time sum(1:1000000)
@assert 2 + 2 == 4

String Operations and File I/O

# Strings
name = "Julia"
"Hello, $name!" # Interpolation
split("a,b,c", ",") # ["a", "b", "c"]

# File I/O
write("file.txt", "Data")
content = read("file.txt", String)

Performance Tips

- Use local variables in functions


- Avoid global variables in computations
- Type stability: make sure return types are predictable
- Use @inbounds, @views for array performance
- Benchmark with BenchmarkTools.jl

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy