Python Learning Week 1

Introduction to​​ Python

 

Background

Installing Python and JetBrains Pycharm:

www.python.org​​ 

http://www.python.org/download/releases/2.7/​​ 

https://www.jetbrains.com/pycharm/

Code Example 1 - Hello, World!

>>> print "Hello, World!“​​ 

 

Math in Python

>>> 1 + 1

2​​ 

>>> 6-5

1​​ 

>>> 2*5

10​​ 

>>> 5**2

25​​ 

>>> 21/3

7

>>> 23%3

2​​ 

 

 

 

Python Operators​​ 

command​​ 

name​​ 

example​​ 

output​​ 

+​​ 

Addition​​ 

4+5​​ 

9​​ 

-​​ 

Subtraction​​ 

8-5​​ 

3​​ 

*​​ 

Multiplication​​ 

4*5​​ 

20​​ 

/​​ 

Division​​ 

19/3​​ 

6​​ 

%​​ 

Remainder​​ 

19%3​​ 

5​​ 

**​​ 

Exponent​​ 

2**4​​ 

16​​ 

 

Order of Operations:

  • parentheses ()​​ 

  • exponents **​​ 

  • multiplication *, division \, and remainder %​​ 

  • addition + and subtraction -​​ 

 

Comments in Python:

>>> #I am a comment. I can say whatever I want!​​ 

Variables:

print "This program is a demo of variables"

v = 1

print "The value of v is now", v

v = v + 1

print "v now equals itself plus one, making it worth", v

print "to make v five​​ times bigger, you would have to type v = v * 5"

v = v * 5

print "there you go, now v equals", v, "and not", v / 5​​ 

 

Strings:

word1 = "Good"​​ 

word2 = "Morning"​​ 

word3 = "to you too!"​​ 

print word1, word2​​ 

sentence = word1 + " " + word2 + " " +word3​​ 

print sentence​​ 

 

Basic Mathematics & Conditional Statements in Python

Boolean operators:

Expression​​ 

Function​​ 

<​​ 

less than​​ 

<=​​ 

less that or equal to​​ 

>​​ 

greater than​​ 

>=​​ 

greater than or equal to​​ 

!=​​ 

not equal to​​ 

<>​​ 

not equal to (alternate)​​ 

==​​ 

equal​​ to​​ 

 

Conditional Statements:

‘if' - Statement​​ 

y = 1

if y == 1:

 ​​ ​​ ​​​​ print "y still equals 1, I was just checking"

‘if - else' - Statement​​ 

a = 1

if a > 5:

 ​​ ​​ ​​​​ print "This shouldn't happen."

else:

 ​​ ​​ ​​​​ print "This should happen."

‘elif' - Statement​​ 

z = 4

if z > 70:

 ​​ ​​ ​​​​ print "Something is very wrong"

elif z < 7:

 ​​ ​​ ​​​​ print "This is normal"

Example

wDa0J+CwJaT+wAAAABJRU5ErkJggg== - Python Learning Week 1

0aNFc60C5Hz1Wk72IPnnXzdXLH+h76kN5xM8egmNYLDrl2HX9bFI8vu8CCF0UcchiM3FjgMLDpK04FBGzIugX1RYQxZCABSri7IneFWW5un6nqh5jmBCA3WVQRrU9sHpMLKKsx6MKBu5zlWJJbj7s7dOAm9MXM+H09LTrX0AtjsPhnQMXh6zHAA5aemrfzwBBKorG7qevCb0c0n7FKd+fJThsrHbXmjAMarIxBA6JVxuDXu5sGAGdAzc8uHJtMQTEq8WgV8cbRkC82vDgyrXFEBCvFoNeHW8YAfFqw4Mr1xZDQLxaDHp1vGEExKsND65cWwwB8Wox6NXxhhEQrzY8uHJtMQTEq8WgV8cbRkC82vDgyrXFEBCvFoNeHW8YAfFqw4Mr1xZDQLxaDHp1vGEExKsND65cWwyBfwCpkXGcheh8EQAAAABJRU5ErkJggg== - Python Learning Week 1

Obtaining data from the user

kS1dCPZuodisONubZ1836GkIe1H5TJzR6m4LqsoE3mSjxyVwGiskhYVyexdQfvuTwlERbpwk6Ui3c2IjVaIsOKGURFK0QiBIQgUrFOOJAUt8Nm+Y65zJwzEJiiOxDb4UmKU5zQ8AUlfvsvuKMzFRAh4BMqqvDljh9dhpT+JmrM6QdlQZuKd2pThCYIhPO3s7AQlFJkQGIJANIdCZ8T249YuQ0MQ11ghIATiCEQjVJyjKIWAEBACYyGwOVXeWIiIjxAQAvNBQBFqPraQJEJACKQIKELJJ4SAEJgvAopQ87WNJBMCQkARSj4gBITAfBFQhJqvbSSZEBACilDyASEgBOaLgCLUfG0jyYSAEFCEkg8IASEwXwT+F4qt0Tbfb6qZAAAAAElFTkSuQmCC - Python Learning Week 1

Tasks:

  • Write a program printline.py that prints second prog the sentence “This is my second program”

  • Print it as a single​​ line

  • Print it with single word on each line

 

  • Which of the following statements are likely to cause problems:​​ 

    • print “This is a valid statement\n”

    • print “This is a valid statement”\n​​ 

    • print “This is a ”valid” statement”​​ 

    • printx “This is a valid statement\n”

 

  • Design a calculator which can perform the Add, Subtract, Multiply and Division operation by using If Else statement?

 

 

Check solution here Python Learning Week 1 (Solution)