Created by Halil Temurtaş, last modified on Jul 06, 2017
print("Hello Intership!!!")
x = 1
if x == 1:
# indented four spaces
print("x is 1.")
myint = 7
print(myint)
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
mystring = "Don't worry about apostrophes"
print(mystring)
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
a, b = 3, 4
print(a,b)
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3
# prints out 1,2,3
for x in mylist:
print(x)
numbers=[]
numbers.append(1)
numbers.append(2)
numbers.append(3)
strings=[]
strings.append("hello")
strings.append("world")
names=["John","Eric","Jessica"]
second_name=names[1]
print(numbers)
print(strings)
print("The second name on the name list is %s" %second_name)
number = 1 + 2 * 3 / 4.0
print(number)
#-----------------------------------------------
#-----------------------------------------------
remainder = 11 % 3
print(remainder)
squared = 7 ** 2
cubed = 2 ** 3
lotsofhellos = "hello " * 10
print(lotsofhellos)
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)
x = object()
y = object()
# This prints out "Hello, John!"
name = "John"
print("Hello, %s!" % name)
# This prints out "John is 23 years old."
name = "John"
age = 23
print("%s is %d years old." % (name, age))
#%s - String (or any object with a string representation, like numbers)
#d - Integers
#%f - Floating point numbers
#%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
#%x/%X - Integers in hex representation (lowercase/uppercase)
xcc=len(name)
print("%d" %xcc)
print(xcc)
astring = "Hello world!"
print(astring.index("o")) #4
print(astring.count("l")) #3
print(astring[3:7]) #lo w 3 to 7 (7th is not included)
print(astring[3:7:2]) #l 3to7 skipping one character
print(astring[::-1])
print(astring.upper())
print(astring.lower())
print(astring.startswith("Hello")) #True
print(astring.endswith("asdfasdfasdf")) #False
afewwords = astring.split(" ")
print(afewwords)
print(afewwords[1])
x = 2
print(x == 2) # pPints out True
print(x == 3) # Prints out False
print(x < 3) # Prints out True
# The "not equals" operator is marked as "!=".
name = "John"
age = 23
if name == "John" and age == 23:
print("Your name is John, and you are also 23 years old.")
if name == "John" or name == "Rick":
print("Your name is either John or Rick.")
name = "John"
if name in ["John", "Rick"]:
print("Your name is either John or Rick.")
#if <statement is="" true="">:
#<do something="">
#....
#....
#elif <another statement="" is="" true="">: # else if
#<do something="" else="">
#....
#....
#else:
#<do another="" thing="">
#....
#....
#</do></do></another></do></statement>
x = [1,2,3]
y = [1,2,3]
print(x == y) # Prints out True
print(x is y) # Prints out False #!!!!!!!
print(not False) # Prints out True
print((not False) == (False)) # Prints out False
first_array=[0]
if first_array: #doğru kabul ediyor
print("22")
else:
print("12")
first_array=0
if first_array: #yanlış kabul ediyor
print("22")
else:
print("12")
primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
temurtas = [5, 8, 3, 6]
for halil in temurtas:
print(halil)
print(temurtas)
for x in range(12,23):
print(x)
count = 0
while count < 5:
print(count)
count += 1 # This is the same as count = count + 1
# Prints out 0,1,2,3,4
count = 0
while True:
print(count)
count += 1
if count >= 5:
break
# Prints out only odd numbers - 1,3,5,7,9
for x in range(10):
# Check if x is even
if x % 2 == 0:
continue
print(x)
# Prints out 0,1,2,3,4 and then it prints "count value reached 5"
count=0
while(count<5):
print(count)
count +=1
else:
print("count value reached %d" %(count))
# Prints out 1,2,3,4
for i in range(1, 10):
if(i%5==0):
break
print(i)
else:
print("this is not printed because for loop is terminated "
"because of break but not due to fail in condition")
# If break statement is executed inside for loop then the "else" part is skipped.
# Note that "else" part is executed even if there is a continue statement.
#-----------------------------------------------
#-------FUNCTIONS-------------------------------
#-----------------------------------------------
def my_function():
print("Hello From My Function!")
def my_function_with_args(username, greeting):
print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))
def sum_two_numbers(a, b):
return a + b
# print(a simple greeting)
my_function()
#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")
# after this line x will hold the value 3!
x = sum_two_numbers(1,2)
print("x=%s" %x)
#-----------------------------------------------
#---------EXERCISE------------------------------
#-----------------------------------------------
def list_benefits():
return "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
return "%s is a benefit of functions!" % benefit
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print(build_sentence(benefit))
name_the_benefits_of_functions()
#-----------------------------------------------
#-------CLASSES and OBJECTS---------------------
#-----------------------------------------------
# Objects are an encapsulation of variables and functions into a single entity.
# Objects get their variables and functions from classes.
# Classes are essentially a template to create your objects.
class MyClass:
variable = "blah"
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass()
myobjecty = MyClass()
myobjecty.variable = "yackity"
print(myobjectx.variable)
print(myobjecty.variable)
myobjectx = MyClass()
myobjectx.function()
#-----------------------------------------------
#---------EXERCISE------------------------------
#-----------------------------------------------
# define the Vehicle class
class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str
# your code goes here
car1 = Vehicle()
car1.name = "Fer"
car1.color = "red"
car1.kind = "convertible"
car1.value = 60000.00
car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00
# test code
print(car1.description())
print(car2.description())
# Output:
# Fer is a red convertible worth $60000.00.
# Jump is a blue van worth $10000.00.
#-----------------------------------------------
#----------------DICTIONARIES-------------------
#-----------------------------------------------
phonebook = {}
phonebook["John"] = 938477566
phonebook["Jack"] = 938377264
phonebook["Jill"] = 947662781
print(phonebook)