This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How would you count the numbers in this list in as few lines of code as possible? | |
numbers = [1, 2, 3, 4, 5] | |
# 1. Charlie sums the numbers up using a for loop: | |
total = 0 | |
for number in numbers: | |
total += number | |
# 2. Bob sums the numbers up using the reduce function: | |
from functools import reduce |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fruit { var name = "Banana" } | |
let fruit1 = Fruit() | |
let fruit2 = fruit1 | |
print(fruit1 === fruit2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = 1 | |
let b = 1 | |
print(a == b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Fruit { | |
let name: String | |
let color: String | |
} | |
var banana = Fruit(name: "Banana", color: "Yellow") | |
print(banana.name, banana.color) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Fruit { | |
let name: String | |
let color: String | |
init(name: String, color: String) { | |
self.name = name | |
self.color = color | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = 1 | |
var b = 2 | |
(a, b) = (b, a) | |
print(a, b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let coords = (x: 1, y: 3, z: 2) | |
let (x, y, z) = coords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let coords = (x: 1, y: 3, z: 2) | |
let x = coords.x | |
let y = coords.y | |
let z = coords.z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
for number in numbers where number % 2 == 0{ | |
print(number) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
for number in numbers { | |
if number % 2 == 0 { | |
print(number) | |
} | |
} |