Sebenta de Introduction to Programming
Introduction:
• What is a program?
o Sequence of instructions to perform a computation.
• Natural vs. Formal languages:
o Natural language is used by people to talk and can be ambiguous.
o Formal language is designed by people for specific applications.
• Computer language:
o Complied.
o Interpreted.
Print Statement:
• Print displays output to your console.
• Function:
o print(‘Hello world’) – either in single quotes or double quotes to enclose your strings.
• Be consistent – always single quotes or always double quotes.
• Getting information from the user:
o Input function:
§ variable=input(‘Please enter your name: ‘)
§ print(variable)
• Printing blank lines can improve readability.
• /n à insert a new line in the middle of the string.
Comments:
• Using comments are a way of documenting your code.
• By adding a hashtag in the beginning of the line.
• It is meant for you as a programmer.
• If you have a single quote inside a string, then we must use double quotes to enclose a string.
• Add comments so that others can understand your code.
• Comments don’t do anything.
Variable Types:
String Concepts:
• Strings can be stored in variables.
• You can use functions to modify strings:
o sentence = ‘some string’.
o print(sentence.upper()) – everything in upper case.
o print(sentence.lower()) – everything in lower case.
o print(sentence.capitalize()) – only first letter of the string in upper case.
o print(sentence.count(‘a’)) – counts the number of times ‘a’ appears in the string.
Numeric Data Types:
• Numbers can also be stores in variables.
• You can do math with numbers.
• Operators:
• str(variable) – converts numeric data type to string.
• The input function will always return a string. There, we have to convert the string to a numerical type
in order to perform operations – by using the int() or float() functions.
Date Data Types:
• The way to have the current time is to use the datatime library.
• In particular, the datetime function as well.
• The timedelta function is in the same library, but allows to state how many days we want from a period
of time.
• If you want to format it differently, we can request just parts of it:
o .day
o .month
o .year
• Sometimes, input from user can also be a date and we need to store it as a date:
o birthday = input(‘When is your birthday: ‘)
o birthday date = datetime.strptime(birthday, ‘%d/%m/%Y’)
o Learn the syntax for strptime function in its documentation.
Boolean Variables:
Conditional Logic:
• The ability to take different actions based on conditions.
• If only one of the conditions will ever occur you can use the elif statement.
• Nest if statements à if statement inside an if statement.
• You can also combine conditions with an and – relation to truth tables.
Lists, Arrays and Dictionaries:
• Lists are collections of items.
• Use square brackets.
o names = [‘Christhoper’, ‘Susan’]
o names.sort – will automatically sort the list in alphabetical order.
• Empty list:
o scores = []
• Add new item to the end:
o scores.append(98)
• We can access individual items of a list by using indexes.
• All counting starts with zero.
• Arrays are also collections of items.
• We have to use: from array import array
• Arrays are designed for simply types such as numbers; and must all be the same type.
• A list, on the other hand, can store anything of any type.
• Dictionaries:
o Key/Value pairs; In lists, we have zero-based index.
o Storage order not guaranteed.
o Empty dictionary: person = {}
Loops:
• For loops and while loops.
Functions:
• Allows to write code more efficiently.
• Functions must have:
o Name.
• Functions may have:
o Argument(s).
o Return value.
• A local variable is a variable defined inside a function. It can only be used or be “visible” inside the
function where it was defined.