Python Basics-1-16
Python Basics-1-16
# Start
print("Hello World!")
Python Comments
Comments starts with a #, and Python will ignore them:
For a comment more then one lone, add a multiline string (triple quotes) in
your code, and place your comment inside it:
#This is a comment
"""
This is a comment
written in
more than just one line
"""
Print & Input
# Display something
print("Hello World!")
• Variable Names
• A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume)
• Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
Variables
Exercise:
Create three variables named “age, Age and AGE” and assign the values
20,30,40 to the them. Print the variables.
Python Data Types
• Variables can store data of different types, and different types can do
different things.
• Python has the following data types built-in by default, in these categories:
• Numeric Types: int, float, complex
• Text Type: str
• Sequence Types: list, tuple, range
• Mapping Type: dict
• Set Types: set
• Boolean Type: bool
Python Numbers
• There are three numeric types in Python: int, float, complex
• Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
• Float, or "floating point number" is a number, positive or negative, containing one or
more decimals.
• Complex numbers are written with a "j" as the imaginary part.
You can get the data type of a variable with the type() function.
String
• Strings in python are surrounded by either single quotation marks, or
double quotation marks.
• Multiline Strings
• You can assign a multiline string to a variable by using three quotes
String
Strings are Arrays
• However, Python does not have a character data type, a single character is
simply a string with a length of 1.
• String Concatenation
• Use + to combine strings
String
• String Format
• Use the format() method to insert numbers into strings
• Exercise:
• Insert the correct syntax to join text and int,
• Print: My name is John, and I am 36
age = 36
txt = "My name is John, and I am "
String
String Methods
• The strip() method removes any whitespace from the beginning or the end.
• The split() method splits the string into substrings if it finds instances of the
separator.
String
• Exercise:
• Make a string with variable name txt, and then:
• Print the length of the string.
• Get the first character of the string
• Convert the value of txt to upper case.
Python Booleans
Booleans represent one of two values: True or False.
• Arithmetic Operators
• Arithmetic operators are used with numeric values to perform common mathematical operations.
• Assignment Operators
• Assignment operators are used to assign values to variables.
• Comparison Operators
• Comparison operators are used to compare two values.
• Logical Operators
• Logical operators are used to combine conditional statement.
Python Operators
Exercise:
Take two numbers from the user, store them in variables, and apply
Arithmetic operators. Print the result.