Day 1 and 2 Flashcards

(23 cards)

1
Q

What is var ? Create list of name of your siblings. Print each.

A

var siblingName = “X”
print(siblingName)
siblingName = “Y”
print(siblingName)
siblingName = “z”
print(siblingName)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is constant and its feature how to create it ? Give one example by Surname.

A

If you don’t ever want to change a value, you need to use a constant .

if you try to change it it would show error

let surname = “X”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is String ? How to assign it ?

A

When you assign text to a constant or variable, we call that a string . emojis included.

Swift’s strings start and end with double quotes

let actor = “Denzel Washington”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you add a quote inside a string ? Give me example .

A

Put a backlash \ before the double quote. “

let quote = “ This is "xyzzy" . “

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to make a string line break ? Write a huge show title in string in 3 separate lines .

A

Using “”” three set of double .

let show = “””
A day in
the life of an
Apple engineer
“””

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to Find length of a string ? find length of full name
1. length using 1 line to code.
2. using var
3. print var
4. print without using the function in the same line

A

using .count after variable/constant/string

  1. “Simran”.count
  2. var name = “Simran”
    name.count
  3. print(name.count)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to uppercase a string ? give example and print.

A

.uppercased()

print(“Martha”.uppercased())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to print whether a string starts with prefix / suffix . Give example of both and print.

A

hasPrefix()
hasSuffix()

print(“A day in my”.hasPrefix(“A day”))
print(“filename.jpg”.hasSuffix(“.jpg”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to store whole numbers ? Give example .

Why differentiate from string ?

A

you store whole numbers / integers by just directly only inputing numbers in var / let .

let score = 10000

You can calculate integers in such constant but in string “It acts like text 100 “ hence you can’t calculate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to store big numbers like 100k in integer so its easy to read.

A

let score = 100_000 . swift doesn’t read underscores.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

banana is 20
mango is 35
all fruits are 77
apple is x
fresh are 23
unfresh is y

use operators to find x y.
by using shorthand operator . add 10, remove 5. multiply 3 . on each operator one by one.

A

var banana = 20
var mango = 35
var allFruit = 77
var fresh = 23

var apple = allFruit - (banana + mango)
var unfresh = allFruit - fresh

banana += 10
mango -= 5
apple *= 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

find if 3 is multiple of 120 and 57 or not .

A

let number = 120
print(number.isMultiple(of: 3)
or
print(120.isMultiple(of: 3))

same with 57.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

can you write integer and decimal together , why or why not ? what are decimals called in swift language and its short form

A

no you cannot write them together . would cause error.

they are different kinds of data like integer is different from the string. once Swift has decided what data type a constant or variable holds, it must always hold that same data type.

decimals are called floating point numbers
and double in short .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

if a = 1 and b = 2.0 . c is addition of both. write code to find c . there are 2 ways .

A

let a = 1
let b = 2.0
let c = a + Int(b) or let c = Double(a) + b

need to tell Swift explicitly that it should either treat the Double inside b as an Int OR treat the Int inside a as a Double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

mutliply 5.0 to 2 ? is it possible since one is double the other is integer,

A

decimal numbers have the same range of operators and compound assignment operators as integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is boolean and what does it store ?

A

boolean is a type of data like strings , integers and decimals. it stores either true or false.

let gameOver = false

17
Q

assign a Boolean’s initial value from some other code

A

let isMulti = 120.isMultiple(of: 3)

18
Q

What kind of operators does boolean have or have not ?

A

Booleans don’t have arithmetic operators such as + and - – after all, what would true + true equal

Booleans do have one special operator, !, which means “not”. This flips a Boolean’s value from true to false, or false to true

toggle() on a Boolean it will flip a true value to false, and a false value to true. that’s the same as using ! just in slightly less code

19
Q

flip a Boolean’s value 2 times
var isAuthenticated = false

using 2 ways.

A

var isAuthenticated = false
isAuthenticated = !isAuthenticated
print(isAuthenticated)
isAuthenticated = !isAuthenticated
print(isAuthenticated)

or
var isAuthenticated = false
print (isAuthenticated)
isAuthenticated.toggle ()
print (isAuthenticated)
isAuthenticated.toggle ()
print (isAuthenticated)

20
Q

how to join strings together and what is it called ?

A
  1. joining them using +,
  2. special technique called string interpolation. which is (x)
    x being the name of a variable or constant
21
Q

show 2 techniques to join 2 strings together.

let name = “Taylor”
let occupation = “singer”
message should be hello my name is Taylor and my occupation is singer

and explain why one is better than other

A

let name = “Taylor”
let occupation = “singer”
let message = “Hello, my name is (name) and my occupation is (occupation).”

or

let message = “Hello, my name is” + name + “and my occupation is” + occupation

Swift can’t join all those strings in one go. Instead, it will join each + one by one. like - “Hello, my name is Taylor” and than join “ Hello, my name is Taylor and my occupation is “ .. etc.

making temporary strings that aren’t used when code finishes. hence string interpolation is better lets us efficiently create strings from other strings, but also from integers, decimal numbers, and put calculations inside string interpolation.

and use string interpolation to add it to string too even if its integer. + lets us add strings to strings, integers to integers, and decimals to decimals, but doesn’t let us add integers to strings .

22
Q

Checkpoint 1 . Write code.

Your goal is to write a Swift playground that:

Creates a constant holding any temperature in Celsius.
Converts it to Fahrenheit by multiplying by 9, dividing by 5, then adding 32.
Prints the result for the user, showing both the Celsius and Fahrenheit values.

A

here are some hints:

  1. Use let to make your constant. You can call it whatever you want, but I think celsius would be an appropriate name.
  2. Celsius is commonly stored as a decimal, so make sure and create it as one. This might mean adding “.0” to the end – using 25.0 rather than 25, for example.
  3. We use * for multiplication and / for division.
    Use (someVariable) to activate string interpolation.
  4. If you want to get fancy with print(), you can use Option+Shift+8 to get the degrees symbol: °. This means you can write something like 25°F.
    import Cocoa

let celcius = 39.0
print(celcius)
let farhenheit = (celcius * 9/5) + 32
print(farhenheit)

print(“The weather is (celcius) ° celsius, or (farhenheit) ° farhenheit.”)