Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

3/4/2021 DS_Program_File_3 - Jupyter Notebook

In [1]: ▾ # DS Exercise 1.4 , variables and types

myint=7
myfloat_1=5.2
myfloat_2=float(9)
mystring_1='hello'
mystring_2="hello"
print(myint)
print(myfloat_1)
print(myfloat_2)
print(mystring_1)
print(mystring_2)

7
5.2
9.0
hello
hello

In [2]: ▾ # DS Exercise 1.5 , operators on numbers and strings

one = 1
two = 2
three = one + two
print(three)

hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)

3
hello world

In [7]: ▾ # DS Exercise 1.6 , assignments

a=3;b=4
print(a);print(b)

a, b = 3, 4
print(a,b)

3
4
3 4

localhost:8888/notebooks/DS_Program_File_3.ipynb# 1/4
3/4/2021 DS_Program_File_3 - Jupyter Notebook

In [12]: ▾ # DS Exercise 1.7 , type casting

one = 1
two= 5.6
print(one +int(two)) # type casting
print(one+two)

6
6.6

In [13]: ▾ # DS Exercise 1.8 , possible error


one=1;
two="hello"
print(one+two)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-a37dc3e0f605> in <module>
2 one=1;
3 two="hello"
----> 4 print(one+two)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [22]: ▾ # DS Exercise 1.9 , print with %d,%f and %s


mystring = "hello"
myfloat = 10.0
myint = 20

▾ if mystring == "hello":
print("String: %s" % mystring)
# print("String:", mystring)
▾ if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
▾ if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)

String: hello
String: hello
Float: 10.000000
Integer: 20

In [1]: ▾ # DS Exercise 1.10 , multiline comments

"""This is a comment
written in
more than just one line"""
print("Hello, World!")

Hello, World!

localhost:8888/notebooks/DS_Program_File_3.ipynb# 2/4
3/4/2021 DS_Program_File_3 - Jupyter Notebook

In [8]: ▾ # DS Exercise 1.11 , casting

x = str(3) # x will be '3'


y = int(3) # y will be 3
z = float(3) # z will be 3.0
print(x,y,z)
print(type(x),type(y),type(z))

3 3 3.0
<class 'str'> <class 'int'> <class 'float'>

In [9]: ▾ # DS Exercise 1.12 , case sensitive


a = 50
A = "Great" #A will not overwrite a
print(a,A)

50 Great

In [10]: ▾ # DS Exercise 1.13 , variable names


myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

In [12]: ▾ # DS Exercise 1.14a , illegal variable names, possible errors


2myvar = "John"

File "<ipython-input-12-808f447f38fe>", line 2


2myvar = "John"
^
SyntaxError: invalid syntax

In [13]: ▾ # DS Exercise 1.14b , illegal variable names, possible errors


my-var = "John"

File "<ipython-input-13-23740dfebf8a>", line 1


my-var = "John"
^
SyntaxError: cannot assign to operator

localhost:8888/notebooks/DS_Program_File_3.ipynb# 3/4
3/4/2021 DS_Program_File_3 - Jupyter Notebook

In [14]: ▾ # DS Exercise 1.14c , illegal variable names, possible errors


my var = "John"

File "<ipython-input-14-ffb5c73e7820>", line 1


my var = "John"
^
SyntaxError: invalid syntax

In [16]: ▾ # DS Exercise 1.15, Multi Words Variable Names


myVariableName = "John" # camel case (Each word, except the first, start
MyVariableName = "John" # Pascal Case (Each word starts with a capital l
my_variable_name = "John" # snake case (Each word is separated by an under

In [17]: ▾ # DS Exercise 1.16


x, y, z = "Orange", "Banana", "Cherry"
print(x,y,z)
x="Orange";y="Banana";z="Cherry"
print(x,y,z)
x = y = z = "Orange"
print(x,y,z)

Orange Banana Cherry


Orange Banana Cherry
Orange Orange Orange

In [18]: ▾ # DS Exercise 1.17 , unpacking a list


fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)

apple
banana
cherry

In [ ]:

localhost:8888/notebooks/DS_Program_File_3.ipynb# 4/4

You might also like