Day 1 and 2 Flashcards
(23 cards)
What is var ? Create list of name of your siblings. Print each.
var siblingName = “X”
print(siblingName)
siblingName = “Y”
print(siblingName)
siblingName = “z”
print(siblingName)
What is constant and its feature how to create it ? Give one example by Surname.
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”
What is String ? How to assign it ?
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 would you add a quote inside a string ? Give me example .
Put a backlash \ before the double quote. “
let quote = “ This is "xyzzy" . “
How to make a string line break ? Write a huge show title in string in 3 separate lines .
Using “”” three set of double .
let show = “””
A day in
the life of an
Apple engineer
“””
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
using .count after variable/constant/string
- “Simran”.count
- var name = “Simran”
name.count - print(name.count)
How to uppercase a string ? give example and print.
.uppercased()
print(“Martha”.uppercased())
How to print whether a string starts with prefix / suffix . Give example of both and print.
hasPrefix()
hasSuffix()
print(“A day in my”.hasPrefix(“A day”))
print(“filename.jpg”.hasSuffix(“.jpg”))
How to store whole numbers ? Give example .
Why differentiate from string ?
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 to store big numbers like 100k in integer so its easy to read.
let score = 100_000 . swift doesn’t read underscores.
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.
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
find if 3 is multiple of 120 and 57 or not .
let number = 120
print(number.isMultiple(of: 3)
or
print(120.isMultiple(of: 3))
same with 57.
can you write integer and decimal together , why or why not ? what are decimals called in swift language and its short form
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 .
if a = 1 and b = 2.0 . c is addition of both. write code to find c . there are 2 ways .
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
mutliply 5.0 to 2 ? is it possible since one is double the other is integer,
decimal numbers have the same range of operators and compound assignment operators as integers
What is boolean and what does it store ?
boolean is a type of data like strings , integers and decimals. it stores either true or false.
let gameOver = false
assign a Boolean’s initial value from some other code
let isMulti = 120.isMultiple(of: 3)
What kind of operators does boolean have or have not ?
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
flip a Boolean’s value 2 times
var isAuthenticated = false
using 2 ways.
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)
how to join strings together and what is it called ?
- joining them using +,
- special technique called string interpolation. which is (x)
x being the name of a variable or constant
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
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 .
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.
here are some hints:
- Use let to make your constant. You can call it whatever you want, but I think celsius would be an appropriate name.
- 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.
- We use * for multiplication and / for division.
Use (someVariable) to activate string interpolation. - 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.”)