Printing your name in large letters

Q: Write a program that prints your name in large letters.

Product of the First Ten Positive Integers

Q: Write a program that prints the product of the first ten positive integers, 1 × 2 × … × 10. (Use * to indicate multiplication in Python.)


Balance of an Account

Q: Write a program that prints the
balance of an account after the first, second, and third year. The account has an initial balance of $1,000 and earns 5 percent interest per year.
balance = 1000
percent = 0.05
for i in range(1,4):
    balance = balance+ balance*percent
    print("The balacne after %d is %.2f"%(i,balance))

How to clone or copy a list?

Question: 
What are the options to clone or copy a list in Python?

While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this?

Best Answer:
With new_list = my_list, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment.
To actually copy the list, you have various possibilities:
  • You can use the builtin list.copy() method (available since Python 3.3):
  • new_list = old_list.copy()
  • You can slice it:
    Alex Martelli's opinion (at least back in 2007) about this is, that it is a weird syntax and it does not make sense to use it ever. ;) (In his opinion, the next one is more readable).
  • new_list = old_list[:]
  • You can use the built in list() function:
  • new_list = list(old_list)
  • You can use generic copy.copy():
    This is a little slower than list() because it has to find out the datatype of old_list first.
  • import copy
    new_list = copy.copy(old_list)
  • If the list contains objects and you want to copy them as well, use generic copy.deepcopy():
    Obviously the slowest and most memory-needing method, but sometimes unavoidable.
  • import copy
    new_list = copy.deepcopy(old_list)
Example:
import copy

class Foo(object):
    def __init__(self, val):
         self.val = val

    def __repr__(self):
        return str(self.val)

foo = Foo(1)

a = ['foo', foo]
b = a.copy()
c = a[:]
d = list(a)
e = copy.copy(a)
f = copy.deepcopy(a)

# edit orignal list and instance 
a.append('baz')
foo.val = 5

print('original: %r\n list.copy(): %r\n slice: %r\n list(): %r\n copy: %r\n deepcopy: %r'
      % (a, b, c, d, e, f))

Result:
original: ['foo', 5, 'baz']
list.copy(): ['foo', 5]
slice: ['foo', 5]
list(): ['foo', 5]
copy: ['foo', 5]
deepcopy: ['foo', 1]

Sum of the first ten positive integers

Q: Write a program that prints the sum of the first ten positive integers.





By Using for loop:
for i in range(0,10):
    print(i+1)


By Using while loop:


n = 1
while(n<=10):
    print(n)
    n+=1


By Using Recursive:


def Sum(CScientists):
    if CScientists == 10:
        return 10
    print(CScientists+1)
    return Sum(CScientists+1)

Start = 0
Sum(Start)

Printing a greeting of your choice


Q: Write a program that prints a greeting of your choice, perhaps in a language other than English.

Input:

  • Your choice number from the menu.
Output: 
  • prints a greeting

The Code:

#CScientists.
print("Please choose one of these: ")
print("1 - Hey\n2 - Hello\n3 - How are you doing?\n4 - Good morning\n5 - Good evening")
Choice = int(input("Please enter your choice: "))
if Choice == 1:
    print("Hey")

if Choice == 2:
    print("Hello")
 
if Choice == 3:
    print("How are you doing?")
 
if Choice == 4:
    print("Good morning")
 
if Choice == 5:
    print("Good evening")

#CScientists.

Variables in Python

Introduction

Numbers and character strings are important data types in any Python program

  • These are the fundamental building blocks we use to build more complex data structures.

Variables

A variable is a named storage location in a computer program.

  1. There are many different types of variables, each type used to store different things.
  2. You ‘define’ a variable by telling the compiler:
    1. What name you will use to refer to it.
    2. The initial value of the variable.
  3. You use an assignment statement (=) to place a value into a variable.

Variable Definition

To define a variable, you must specify an initial value.

****

The assignment statement


  1. Use the assignment statement '=' to place a new value into a variable cansPerPack = 6 # define & initializes the variable cansPerPack
  2. The left-hand side of an assignment statement consists of a variable
  3. The right-hand side is an expression that has a value
  4. Beware: The “=“ sign is NOT used for comparison:
    1. It copies the value on the right side into the variable on the left side
    2. You will learn about the comparison operator in the next chapter

Assignment syntax

*****

Why different types?


Introduction to Python

Python is a programming language. It can be used on a server to create web applications.

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
  1. web development (server-side).
  2. software development.
  3. mathematics.
  4. system scripting.

What can Python do?

  1. Python can be used on a server to create web applications.
  2. Python can be used alongside software to create workflows.
  3. Python can connect to database systems. It can also read and modify files.
  4. Python can be used to handle big data and perform complex mathematics.
  5. Python can be used for rapid prototyping, or for production-ready software development.

Why Python?

  1. Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
  2. Python has a simple syntax similar to the English language.
  3. Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
  4. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
  5. Python can be treated in a procedural way, an object-orientated way or a functional way.

Python Example:

print("CScientists.com")

Output:
CScientists.com