Você está na página 1de 47

LEARNING TO CODE IN

How many
programming
languages do you
know?
SAY, APART FROM C, JAVA AND OBVIOUSLY, PYTHON.

Wednesday, December 24, 2014

What do you mean by


High-Level or LowLevel Languages?
C, C++, JAVA

ASSEMBLY LANGUAGES

Wednesday, December 24, 2014

Advantages or
Disadvantages of
being High/Low level?
SPEED OF EXECUTION/EASE OF
UNDERSTANDING/PORTABILITY

Wednesday, December 24, 2014

Do you know what


kind of languages
they are?
OBJECT-ORIENTED, FUNCTIONAL, PROCEDURAL,
DECLARATIVE
THESE ARE CALLED PROGRAMMING PARADIGMS

Wednesday, December 24, 2014

What makes up a
Program?
ITS CONSTITUENT PARTS.

WHAT KIND OF INSTRUCTIONS, THAT IS

Wednesday, December 24, 2014

What do Computer
Scientists do?
IN A VERY BROAD SENSE OF THE TERM COMPUTER
SCIENTISTS

Wednesday, December 24, 2014

What do Computer Scientists do?


Like mathematicians, computer scientists use formal languages to denote ideas
(specifically computations).

Like engineers, they design things, assembling components into systems and
evaluating tradeoffs among alternatives.
Like scientists, they observe the behavior of complex systems, form hypotheses,
and test predictions.

December 24, 2014

CSI GECA CHAPTER

About Python
THE LANGUAGE WE ARE GOING TO LEARN

Wednesday, December 24, 2014

Python
Python was conceived in the late 1980s by Guido van Rossum.
Its first implementation was started in 1989

Python is a widely used general-purpose, high-level programming language.


Python supports multiple programming paradigms

Object Oriented
Imperative
Functional Programming
Procedural Styles.

Wednesday, December 24, 2014

10

Versions of Python
Version Number

Year of Release

0.9.0 through 1.6

1991-1999

2.0

2000

3.0

2008

Wednesday, December 24, 2014

11

Version 2 or 3?
Wednesday, December 24, 2014

12

Philosophy of Python
The core philosophy of the language
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Readability counts.

Wednesday, December 24, 2014

13

Learning a Language
ANY LANGUAGE.

Wednesday, December 24, 2014

14

Natural and Formal Languages?


oNatural languages are the languages people speak, such as English,
Spanish, and French.

oFormal languages are languages that are designed by people for


specific applications.
oMathematicians
oChemists
oComputer Scientists

Wednesday, December 24, 2014

15

DIFFERENCES?
NATURAL LANGUAGE

FORMAL LANGUAGE

AMBIGUIOUS - deal with by using contextual


clues and other information.

CLEAR statement has exactly one meaning,


regardless of context.

REDUNDANCY to make up for ambiguity and


reduce misunderstandings.

CONCISE Formal languages are less


redundant and more concise.

LITERALNESS Natural languages are full of


idiom and metaphor.

LITERALNESS Formal languages mean exactly


what they say.

Wednesday, December 24, 2014

16

Suggestions for reading programs


Formal languages are much more dense than natural languages, so it takes
longer to read them.

The structure is very important, so it is usually not a good idea to read from top
to bottom, left to right.
learn to parse the program in your head, identifying the tokens and interpreting
the structure.
The details matter!
No Spelling mistakes.
No punctuation errors!

Wednesday, December 24, 2014

17

How do we learn a language?


First off, we learn the characters.
Then we go and learn how they form words.

Then we move onto bigger words and phrases, eventually, sentences.


Once we start learning how to form sentences, we also learn about Grammar.

What is grammar?

Wednesday, December 24, 2014

18

Example: English
CHARACTERS:

ABCDEFGHIJKLMNOPQRSTUVWXYZ , . : ; ?

WORDS:

APPLE

BALL

CAT

DOG\

Bigger Words:

ABHORRENCE

BENEFICIARY

COMPREHENSION

DEBUGGING

PHRASES:

CAT ATE MOUSE

SENTENCES:

DOG CHASED THE CAT BECAUSE IT ATE THE MOUSE, WHICH WAS DWELLING IN
THE HOUSE.

Wednesday, December 24, 2014

DOG CHASED CAT

19

Features of a language?
Syntax Tokens Basic elements that make up words and sentences along
with symbols.
Structure The way tokens are arranged.

Semantics What the sentence means?

Wednesday, December 24, 2014

20

Learning Python
BREAKING IT DOWN

Wednesday, December 24, 2014

21

Tokens
Characters: [a-z A-Z 0-9]
Operators: [/ , //, **, *, +, -, %, =, >, <, >=, <=, ==, !=]
Variables
Keywords

Wednesday, December 24, 2014

22

Variables
VARIABLES AND KEYWORDS FORM TOKENS

Wednesday, December 24, 2014

23

Variables
A variable is a name that refers to a value.
An assignment statement creates new value and gives them variable names:

December 24, 2014

CSI GECA CHAPTER

24

Variables as Memory Locations

int a = 2;

Wednesday, December 24, 2014

a = 2;

int b = a

25

Variables as Names or identifiers

a=1

Wednesday, December 24, 2014

a=2

b=a

26

Special Variable
In interactive mode, the last printed expression is assigned to the variable _.

This variable should be treated as read-only by the user.

December 24, 2014

CSI GECA CHAPTER

27

Keywords
WORDS WHICH ARE RESERVED AND CAN NOT BE USED
AS INDENTIFIERS

Wednesday, December 24, 2014

28

Keywords
1.

'False'

12. 'elif'

23. 'lambda'

2.

'None'

13. 'else'

24. 'nonlocal'

3.

'True'

14. 'except'

25. 'not'

4.

'and'

15. 'finally'

26. 'or'

5.

'as'

16. 'for'

27. 'pass'

6.

'assert'

17. 'from'

28. 'raise'

7.

'break'

18. 'global'

29. 'return'

8.

'class'

19. 'if'

30. 'try'

9.

'continue'

20. 'import'

31. 'while'

10. 'def'

21. 'in'

32. 'with'

11. 'del'

22. 'is'

33. 'yield'

December 24, 2014

CSI GECA CHAPTER

29

Remembering Keywords
>>>Import keyword
>>>keyword.kwlist
>>>keyword.iskeyword(string)
>>>dir(keyword)

Wednesday, December 24, 2014

30

Operators
AS SYMBOLS ARE IN MATHEMATICS AND CHEMISTRY,
OPERATORS ARE IN PROGRAMMING LANGUAGES.

Wednesday, December 24, 2014

31

Operators
1.

(Division)

9.

2.

//

(Floor Division)

10. <

3.

** (Exponentiation)

11. >=

4.

(Multiplication)

12. <=

5.

(Addition)

13. ==

6.

(Subtraction)

14. !=

7.

(Modulus)

8.

(Assignment)

December 24, 2014

>

CSI GECA CHAPTER

32

Data Types
EVERYTHING IS AN OBJECT.

Wednesday, December 24, 2014

33

Basic Data Types


int

1,

31231,

123491348

Float

1.0, 3.1415,

0.13123

Complex

1+3j, 0+0j,

3-2j

Try these out!


Wednesday, December 24, 2014

34

Other Data Types


Strings

abc,

0123,

Lists

[1, 2, 3]

*ab, cd+ *ab, 2, 3+

Tuples

1, 2, 3

b, abc

Dictionaries

,1:a, 2:b, 3:cdef-

Wednesday, December 24, 2014

hello

1, a, abc

35

Identifying Data Types dynamically


Simple!
Use type()
>>>a = 2

>>>type(a)

Wednesday, December 24, 2014

36

Expressions
EXPRESSIONS AS PHRASES.

Wednesday, December 24, 2014

37

Expressions
a+b/c-d*e
(a+b)/(c-d)*e

Wednesday, December 24, 2014

38

Input
input() Method:
variable = input(String: )
But the return type is string.
To convert into integer, use int() method.

Wednesday, December 24, 2014

39

What are
programming
languages used for?
FOR PROBLEM SOLVING!

Wednesday, December 24, 2014

40

Problem and its


Solution
TODAYS PROBLEM

Wednesday, December 24, 2014

41

Write a Temperature
converter
CONVERT TEMPERATURE FROM FAHRENHEIT TO
CELSIUS
AND VICE VERSA

Wednesday, December 24, 2014

42

Tc=(5/9)*(Tf-32)
Tf=(9/5)*Tc+32
FORMULA FOR CONVERSION

Wednesday, December 24, 2014

43

Sample session
Temperature converter

Enter a temperature: 20
Convert to (F)ahrenheit or (C)elsius? F
20 C = 68 F

Wednesday, December 24, 2014

44

Doubts and/or
queries?
ASK AWAY!

Wednesday, December 24, 2014

45

Suggestions?
Tell Us!
DROP A MAIL TO:

PYCLUBGECA@GMAIL.COM

OR A COMMENT ON: BIT.LY/PYCLUBGECA

Wednesday, December 24, 2014

46

Thank You!
PYCLUB GECA TEAM THANKS YOU!

Wednesday, December 24, 2014

47

Você também pode gostar