Python for Absolute Beginners
"Hello, World!" and Beyond
Created by Juan Manuel Torres / @onema
Create a GitHub Account
What is GitHub?
- GitHub is a hosted Git service (version control system)
- It is very popular for hosting Open Source projects
- It is used to "Sign in with GitHub" to a lot of development Tools
Create a GitHub Account
Pick a Username / Password & SELECT A FREE ACCOUNT
Fork the Repository
What is a Computer Program?
Computer programs are collections of instructions that tell a computer how to interact with the user, the computer hardware or process data [1]
What is PYTHON?
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. [Used ] for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together.
Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance.
Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed. [2]
http://redac.socialinput.fr/wp-content/uploads/2013/01/Albino_Burmese_Python.jpg
Writing our first program
"Hello, World!"
The "Hello, World!" program is the simplest program you can write in any programming language. All it does is output "Hello, World!" [3]
Writing our first program
"Hello, World!"
#!/usr/local/bin/python3.5
print('Hello, World!')
With our project there is a sample 01hello.py file
Function to "print" to the screen
Anything in the quotes is a "string"
Used before the script is run, ignored my python
Variables
A variable is a storage location associated with a name. Variables contain an unknown quantity or information called value [4]
# 01variables.py
variable = 'Juan'
print(variable)
print(type(variable))
print(id(variable))
variable = 1
print(type(variable))
print(id(variable))
variable = 1234.5678
print(type(variable))
Rules for Python Variables [5]
Variables names must start with a letter or an underscore, such as:
- _underscore
- underscore_
The remainder of your variable name may consist of letters, numbers and underscores.
- password1
- n00b
- un_der_scores
Names are case sensitive.
- case_sensitive, CASE_SENSITIVE, and Case_Sensitive are each a different variable.
Concatenating Variables
#!/usr/local/bin/python3.5
# 03variables.py
name = 'Juan'
numbers = 12345
hello = 'Hello, {0}! {1}'.format(name, str(numbers))
print(hello)
Data Types
A Data Type Is classification identifying one of various types of data [6]
Data Types
Python defines several data types [7]
- Four scalar types:
- boolean (True or False)
- integer (negative & positive non factional numbers & zero)
- float (floating-point number)
- String (str) (a series of characters)
- Collection types:
- List (collection of object arranged by a numeric key)
- Dictionary (collection of objects arranged by a string key)
Data Types
#!/usr/local/bin/python3.5
# 04datatypes.py
print(type(True))
print(type(12345))
print(type(5678.90123))
print(type([1, 2, 3]))
print(type({'a': 1, 'b': 2, 'c': 'something'}))
Operators
An operator in a programming language is a symbol that tells the [computer] to perform specific mathematical, relational or logical operation and produce final result. [8]
An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value. [9]
Arithmetic Operators
-a | Negation | Opposite of a. |
a + b | Addition | Sum of a and b. |
a - b | Subtraction | Difference of a and b. |
a * b | Multiplication | Product of a and b. |
a / b | Division | Quotient of a and b. |
a % b | Modulus | Remainder of a divided by b. |
a ** b | Exponentiation | Result of raising a to the b'th power. |
Assignment Operator
#!/usr/local/bin/python3.5
# 05operators.py
a = 1;
b = 2;
three = a + b;
print(three)
The assignment operator "=" is used to assign a value to a variable. It is not used to compare!
Comparison Operators
$a == $b | Equal | TRUE if $a is equal to $b |
$a === $b | Identical | TRUE if $a is equal to $b |
$a != $b | Not equal | TRUE if $a is not equal to $b |
$a !== $b | Not identical | TRUE if $a is not equal to $b |
$a < $b | Less than | TRUE if $a is strictly less than $b . |
$a > $b | Greater than | TRUE if $a is strictly greater than $b . |
$a <= $b | Less than or equal to | TRUE if $a is less than or equal to $b . |
$a >= $b | Greater than or equal to | TRUE if $a is greater than or equal to $b . |
Comparison Operators
#!/usr/local/bin/python3.5
# 06comparison_operators.py
print(1 == 2) # FALSE
print(1 < 2) # TRUE
print(1 <= 2) # TRUE
print(2 > 2) # FALSE
print(2 >= 2) # TRUE
print(1 == True) # TRUE
print(1 != True) # FALSE
The assignment operator "=" is used to assign a value to a variable. It is not used to compare!
Let's write some code!
Write a program that does the following:
- Choose a number between 1 and 10
- Assign that number to the variable x
- Add five to x
- Double the result
- Subtract 4
- Divide by 2
- Subtract the original number
practical Advice [10]
- Plan your code before you write it
- Write a lot of code
- Ask others to review your code
- Learning how to program takes time; take the time to learn
- There are a lot of free resources, but don't be afraid to invest in your education (books, trainings, conferences, subscriptions )
questions?
THE END
Juan Manuel Torres | @onema
References
Python for Absolute Beginners
By Juan Manuel Torres
Python for Absolute Beginners
- 1,757