Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python - Variables, Data Types & Operators

Python - Variables, Data Types & Operators

Python - Variables, Data Types & Operators

This slide introduces the basics of programming in Python with variables, data types & operators.
This slide is a part of a the Course "Learn Python, Django & Web Development".
Read More here https://github.com/kabirbaidhya/learn-python-django-web.

Kabir Baidhya

March 16, 2017
Tweet

More Decks by Kabir Baidhya

Other Decks in Technology

Transcript

  1. Reflec ons What we've learned from the past lessons. 1.

    Python - Hello World! 2. Git 3. Version Control 4. GitHub Note: If you're not aware of these. Read them at https://github.com/kabirbaidhya/learn-python-django-web
  2. A variable is a symbolic name for (or reference to)

    information. The variable's name represents what information the variable contains. “ “ http://www.cs.utah.edu/~germain/PPS/Topics/variables.html
  3. For Instance foo and bar could be variables that are

    just symbolic names that represent some information in the memory. # Variable foo contains "Some Information" foo = "Some Information" # And variable bar contains 15.04 bar = 15.64 # Now variable `foo` can be used to get the reference of that "So print("It contains", foo) # And they could be used in expressions too print("Result = ", 5 * bar + 2)
  4. Variables in Python Dynamically typed. Every variable is an object.

    Names are case sensitive. Assigned to a value using = operator eg: foo = 10 . Name can contain letters, underscores ( _ ) followed by numbers. Naming Conven on: lowercase names using underscore _ to separate words. eg: first_name , last_name , etc.
  5. For instance Let's say we have a numeric value 20.0

    and we want to store it as the radius of a circle. We do that like this. radius = 20.0 Here, 1. radius is a variable. 2. 20.0 is the value assigned to that variable.
  6. For instance Suppose that we need to store another constant

    value 3.14 as pi. We can do that as well. PI = 3.14 Now, PI is another variable that holds 3.14.
  7. Using them in expressions Let's do some computation with these

    values we have now. # Values we have radius = 20.0 PI = 3.14 # Compute area of the circle area = PI * radius * radius # Print the results print("Area of Circle =", area)
  8. In a Nutshell A variable is a symbol that stand

    in for a value in the program.
  9. Example 1 first_name = "Kabir" last_name = "Baidhya" home_town =

    "Kathmandu, Nepal" print("Hi! I am", first_name, last_name, ".") print("I'm from", home_town, ".") Output: Hi! I am Kabir Baidhya . I'm from Kathmandu, Nepal .
  10. Buit‐in Data types Numeric: int, oat, long Boolean: bool Sequences:

    str, list, tuple, bytes Mappings: dict Sets: set, frozen set
  11. Immutable & Mutable types 1. Immutable types int, oat, long,

    str, tuple, frozen set, etc. 2. Mutable types list, dict, set, etc.
  12. Integer Integers are positive or negative whole numbers with no

    decimal points. In python 2.x there are two int types: int and long . But in python 3.x onwards both have been uni ed into int and it behaves as long . total_lessons = 24
  13. Boolean Variables with boolean type can represent only two values

    True or False . success = True failure = not success
  14. Example 2: Basic data types an_integer = 6 a_floating_point =

    17.60 a_boolean = True a_string = "Foo" print("Integer value =", an_integer) print("Float value =", a_floating_point) print("Boolean value =", a_boolean) print("String value =", a_string) Output: Integer value = 6 Float value = 17.60 Boolean value = True String value = Foo
  15. Arithme c Operators Python supports all the basic arithmetic operators

    just like any other programming languages. Operator Opera on Example + Addition a + b _ Subtration a - b * Multiplication a * b / Division a / b ** Exponentiation a ** b % Modulo a % b
  16. Comparison Operators Common comparison operators in python are < ,

    > , == , >= , <= , and != . All of these operators return boolean results. Operator Comparison Example > Is greater than a > b < Is less than a < b == Is equal to a == b >= Is greater than or equal to a >= b <= Is less than or equal to a <= b != Is not equal to a != b
  17. Logical Operators All of these operators return boolean results. Operator

    Opera on Example and Logical AND a and b or Logical OR a or b not Logical NOT not a
  18. Example 3 Try these in Python shell. >>> (1 *

    4) + (4 / 2) - (3 * 2) >>> 7 % 3 >>> 2 ** 3 >>> 1 > 2 >>> 1 >= 1 >>> 1 < 5 >>> 1 <= 5 >>> 7 == 5 >>> 8 != 5 >>> 7 > 2 and 5 < 8 >>> 7 > 10 or 5 < 8 >>> not (5 > 7)
  19. Exercise 1 Write a program to calculate the diameter, circumference,

    and the area of circle using the value of radius and constant PI = 3.14159 .
  20. Exercise 2 Write a program to calculate the distance between

    two points represented by coordinates (x1, y1) and (x2, y2) respec vely.
  21. Exercise 3 Write a program to compute the possible values

    of x from a quadra c equa on ax2 + bx + c = 0 (a ≠0) using the quadra c equa on formula.
  22. Input Getting the user input is an important part of

    every program. In python you can use the input function to get the user input easily. # Get the input and store it in the variable. name = input("Please enter your name: ") # Print the entered value. print("Hi", name) Note: input only works in python3. You need to use raw_input if you're using python2.
  23. Exercise 4 Make changes in the program you wrote for

    exercise 1 to get the radius from the user.
  24. Exercise 5 Make changes in the program you wrote for

    exercise 2 to get x1, y1 & x2, y2 from the user.
  25. Exercise 6 Make changes in the program you wrote for

    exercise 3 to get the values of a, b & c from the user.
  26. Exercise 7 Write a program that asks for a number

    and prints if it's an even number or not.