GithubHelp home page GithubHelp logo

pythondemo's Introduction

Learning Python

Printing in python

print 'Hello world'

Creating Variables

Python uses underscores instead of camel case for variables

my_variable = 10

Booleans

Booleans are capitalized. Operators are and or and not

my_bool = True

Functions

Functions are defines using def

def function_name():
    print 'Hello'

Whitespace

Whitespace is important in python to structure code. This is broken.

def spam():
eggs = 12
return eggs

print spam()

to fix it you need to indent. Indentation should be 4 spaces

def spam():
    eggs = 12
    return eggs

print spam()

Comments

To comment out a single line we use the # sign. To comment out multiple lines we use triple single/double quotes """ and end it without another """

'''Some comment here'''

Math

The only new math operation we have is ** (exponentiation) compared to JavaScript.

eggs = 10 ** 2

Strings

Same as JS. \ to escape. Concat is the same +.

'There\'s a snake in my boot!'

Some string methods : len()

parrot = 'Norwegian Blue'
print len(parrot) #14 length of string

String.lower() and String.upper()

parrot = 'Norwegian Blue'
print parrot.lower() #'norwegian blue' lower case opposite for upper

str()

pi = 3.14
print st(pi) #'3.14' as a string

Note: Methods that dot are methods only available to a specific data type. Methods that you envelop in parens apply to more than one data type.

Similar to template literals in JavaScript Python uses string formating with a %.

string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2) # Prints Let's not go to Camelot. 'Tis a silly place.

Note: you need the same number of %s as terms after the string aka after the % (var1, var2, var3)

Conditionals & Control Flow

answer = "Left"
if answer == "Left": # Colon after conditional statement
    print "This is the Verbal Abuse Room, you heap of parrot droppings!"
elif answer == "Right":
    print "Something else"
else:
    print "Parrots...why..."

Comparators : ==, !=, <, >, <=, >= Boolean Operators : and, or, not

pythondemo's People

Contributors

dye784 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.